-1

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

enter image description here

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • This question might already have an answer here: https://stackoverflow.com/questions/50918251/how-to-destructure-nested-objects-in-for-loop#50918381 – Brian Oct 22 '19 at 18:40
  • "Do I have to do a reduce on the array?" one possibility. Destructuring is not meant for variable size structures, and logically, i don't see how you would imagine this to give you more than the first element of `shoes`. – ASDFGerte Oct 22 '19 at 18:43
  • @Brian that actually answered my question; however, it wasn't relevant to the code I had already had before it. But thank you anyway for your assistance. – DragoomDoc99 Oct 22 '19 at 18:54
  • @ASDFGerte I want to have efficient code. I am new to coding however, endless If else loops make reading and understanding code hard. I appreciate you input, and I will apply it for the future. – DragoomDoc99 Oct 22 '19 at 18:54

1 Answers1

1

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)
  }
}
marzelin
  • 10,790
  • 2
  • 30
  • 49