用另一个数组中的值填充数组

时间:2018-11-05 22:37:45

标签: javascript angular

我试图用另一个数组中的值填充一个数组。这就是我现在拥有的:

  public data: any[] = [{
  {
    text: 'Category', items: [
      { text: 'All Categories' },
      { text: 'Cat1' },
      { text: 'Cat2' },
      { text: 'Cat3' },
    ]
  }, {
    text: 'Customers', items:
      this.clients_name.map(text => ({ text }))
  }];

其中clients_name = ['a', 'b', 'c', 'd']

我希望在console.log data[1]上是:

items: (4) […]
0: Object { text: "a" }    ​​
1: Object { text: "b" }
2: Object { text: "c" }
3: Object { text: "d" }
length: 4
<prototype>: Array []
text: "Customers"

但我明白了

items: []
length: 0
<prototype>: Array []
text: "Customers"

为什么不能正确填充data数组?谢谢您的时间!

1 个答案:

答案 0 :(得分:0)

看起来像这样。clients_name是空数组,请在执行此操作之前尝试对其进行初始化。

相关问题