0

I need help understanding this specific code:

stocks[item[0]].inventory

How did the code above accessed or pin-pointed what needed to be pin-pointed using this “[item[0]]” on the stocks object? Can you please teach me how to pin-point thru the properties of an object in this way? What keywords or what can I search in Google to see more explanation for this? WHAT and HOW the logic works on this? Thank you!

const stocks = {
    Mangoes : {
      inventory: 300,
      cost: 2.00
    },
    'Star Apple' : {
      inventory: 400,
      cost: 3.00
    },
    'Jack Fruit' : {
      inventory: 300,
      cost: 4.00
    },
    Apples : 100,
    cost: 6.00
  }

const fruits = {
    items : [['Mangoes', 50],['Apples', 30]]
  }

  fruits.items.every((item) => {
    console.log(stocks[item[0]].inventory >= item[1])
  })

Well, I can used Object.entries, keys, or values to alternatively do that but I can'treally seem to understand how the logic of this code works stocks[item[0]].inventory

  • Each `item` is an array composed of 2 values. `item[0]` is a string. Putting that in bracket notation when looking up on an object gives the associated property value on the object if there is one – CertainPerformance Dec 22 '22 at 20:34
  • `Apples` doesn't have its own object containing inventory/cost in your stock data btw so your result will be... unexpected. – Andy Dec 22 '22 at 20:38

0 Answers0