简体   繁体   中英

Fill array with values from another array

I am trying to fill an array with values from another array. This is what I have by now:

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

where clients_name = ['a', 'b', 'c', 'd']

I expect on console.log data[1] to be:

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

but I get

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

Why isn't filling the data array properly? Thank you for your time!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM