Cannot read property 'push' of undefined when combining arrays

前端 未结 8 2016
轮回少年
轮回少年 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:29

    answer to your question is simple order is not a object make it an array. var order = new Array(); order.push(/item to push/); when ever this error appears just check the left of which property the error is in this case it is push which is order[] so it is undefined.

    0 讨论(0)
  • 2021-02-01 12:29

    I fixed in the below way with typescript

    1. Define and initialize firest

    pageNumbers: number[] = [];

    1. than populate it

      for (let i = 1; i < 201; i++) {
          this.pageNumbers.push(i);
      }
      
    0 讨论(0)
  • 2021-02-01 12:36

    This error occurs in angular when you didn't intialise the array blank.
    For an example:

    userlist: any[ ];
    this.userlist = [ ]; 
    

    or

    userlist: any = [ ];
    
    0 讨论(0)
  • 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]);
    
    0 讨论(0)
  • 2021-02-01 12:37

    order[] is undefined that's why

    Just define order[1]...[n] to = some value

    this should fix it

    0 讨论(0)
  • 2021-02-01 12:51

    In most cases you have to initialize the array,

    let list: number[] = [];
    
    0 讨论(0)
提交回复
热议问题