简体   繁体   中英

Deeply nested Arrays and objects

I am trying to destructure an object. There is an array of objects I would like to pull into the assignment. Do I have to do a reduce on the array?

I tried to destructure the object but it only shows the first object in the Shoes Array.

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)
}

在此处输入图像描述

You have to do another loop for shoes array:

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

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