簡體   English   中英

如何將數組推入嵌套數組?

[英]How to push arrays into a nested array?

我正在嘗試將多個數組推入列部分。

  .then(body => {
    this.options = {
      padding: {
        left: 100,
        right: 100
      },
      data: {
        columns: [
        ],
      }
     }
    }

我有一個包含數組列表的數組。

let new_array = {['test1', 2,3,5,6], ['test2', 5,3,9,6], ['test3', 1,2,5,2]}

我試圖編寫一個將數組推入列的函數,但是我不確定自己做錯了什么。 我不確定如何將事情推入列中。

function populateColumn(new_array) {
  for (var i in new_array) {
    this.options['data']['columns'].push(new_array[i]);
  }
}

我該如何完成?

如果需要平面數組,請使用this.options['data']['columns'].push(...new_array[i]); 而不是this.options['data']['columns'].push(new_array[i]);

或使用this.options['data']['columns'] = this.options['data']['columns'].concat(new_array[i])

是的,並且new_array應該是一個數組,而不是對象。

您可以使用傳播運算符

 const options = { data: { columns: [] } }; const new_array = [ ["test1", 2, 3, 5, 6], ["test2", 5, 3, 9, 6], ["test3", 1, 2, 5, 2] ]; function populateColumn(array) { options.data.columns = [...array]; } populateColumn(new_array); console.log('options.data.columns:', options.data.columns); 

您似乎有一個無效的對象,您可能想從以下位置更新new_array

let new_array = {['test1', 2,3,5,6], ['test2', 5,3,9,6], ['test3', 1,2,5,2]}

let new_array = [['test1', 2,3,5,6], ['test2', 5,3,9,6], ['test3', 1,2,5,2]]

工作演示: https//stackblitz.com/edit/angular-qjwfcq

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM