Cannot read property 'push' of undefined when combining arrays

前端 未结 8 2033
轮回少年
轮回少年 2021-02-01 12:18

When pushing an array\'s contents to another array I get

\"Uncaught TypeError: Cannot read property \'push\' of undefined\" error in this snippet.

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 12:37

    order is an Object, not an Array().

    push() is for arrays.

    Refer to this post

    Try this though(but your subobjects have to be Arrays()):

    var order = new Array();
    
    // initialize order; n = index
    order[n] = new Array();
    
    // and then you can perform push()
    order[n].push(some_value);
    

    Or you can just use order as an array of non-array objects:

    var order = new Array();
    
    order.push(a[n]);
    

提交回复
热议问题