深度嵌套的数组和对象

时间:2019-10-22 18:35:57

标签: javascript

我正在尝试破坏对象。我想将一系列对象放入分配中。我必须对数组进行归约吗?

我试图破坏对象,但是它只显示了鞋数组中的第一个对象。

const currentInventory = [
  {
    name: 'Brunello Cucinelli',
    shoes: [
      {name: 'tasselled black low-top lace-up', price: 1000},
      {name: 'tasselled green low-top lace-up', price: 1100},
      {name: 'plain beige suede moccasin', price: 950},
      {name: 'plain olive suede moccasin', price: 1050}
    ]
  },
  {
    name: 'Gucci',
    shoes: [
      {name: 'red leather laced sneakers', price: 800},
      {name: 'black leather laced sneakers', price: 900}
    ]
  }
];

for(var{name:designerName, shoes:[{name:shoeName,price}]} of currentInventory){
    console.log (designerName, shoeName, price)
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您必须为shoes数组做另一个循环:

for (var { name: designerName, shoes } of currentInventory) {
  for (var { name: shoeName, price } of shoes) {
    console.log(designerName, shoeName, price)
  }
}