Accessing thru Objects without using Object methods

(As per advice to be more precise and clear in asking questions I edited this post for that purpose)

Hello Everyone,

I need help understanding this specific code:

stocks[item[0]].inventory >= item[1]

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 navigate thru the properties of an object in this way? 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])

Please make sure that you correctly format any code you post when asking for help on the forums, it makes things much easier.

It didn’t. You’re calling the .every() method, which is doing the heavy lifting.

The arrow-notation function being passed into .every() is the predicate; this is the test that .every() will perform against each item in fruits. If they all pass the test - i.e. they all return truthy values - you get true, otherwise you get false.

In that code as it is, you may be seeing an output of true on the console… however that is the output of the console.log() call in the predicate, and is not actually the result of the call to .every():slight_smile:

Hello, thepitycoder. Thank you for the help! Can you please also explain to me how the
worked to pinpoint that part of an object to be compared to the number of orders of the const fruits?

PS: Yes I will format it properly from now on. Newbie here =)

No problem.


stocks is an object, so we access the items within stocks by name. So if we wanted to get the Mangoes item from within stocks, we would do stocks["Mangoes"] or stocks.Mangoes. (Aside: Mangoes is a property of the stocks object.)

Each of the items within stocks is also an object, so we can again access their items by name.

The item object is an array, so we access its data by index: item[0] for example.

So, if we’re concerned with the code stocks[item[0]].inventory we have a few things to work out:

1. What is item[0]?

Well, item is the sub-item of the items array from fruits.items which is being passed in by the .every() method. It will be either ["Mangoes", 50] for the first item in fruits.items, or ["Apples", 30] for the second item. Let’s assume we’re working with the first one.

Now we know that item === ["Mangoes", 50], we know that item[0] === "Mangoes" and item[1] === 50.

With that, we now know that stocks[item[0]].inventory === stocks["Mangoes"].inventory

2. Ok, so now what?

Right, so let’s look at what’s in stocks["Mangoes"]. It’s an object:

Mangoes : {
    inventory: 300,
    cost: 2.00

inventory is a property of the Mangoes object, so again we can access it by either Mangoes["inventory"] or Mangoes.inventory.

So, ultimately, stocks[item[0]].inventory being the same as stocks["Mangoes"].inventory is accessing the Mangoes property of stocks, and then the inventory property of Mangoes in turn.

That has a value of 300, which we can compare with item[1] which we know is 50 from earlier.

JS will do the same for the Apples item as well.

Does that make sense?

Hello Mr. thepitycoder,

I wanna thank you for sharing a piece of your time to answer my question. I really appreciate it. I was spending 2 days already to figure this out. I was desperate, so I decided to ask someone and luckily you’ve seen this and answered. Now I know the answer and this will give my brain a small rest in my learning journey.

It took a couple of hours for me to reply as I was thinking about something again. This is the [0] in the stocks[item[0]].inventory. I was wondering why there should be a [0] in this code when stocks[item].inventory might suffice. I experimented around myself to get the answer (I don’t wanna ask too much if I can try to figure it out myself).

const stocks = {

Mangoes       : { inventory: 300, cost: 2.00}, 
'Star Apple'  : { inventory: 400, cost: 3.00},
'Jack Fruit'  : { inventory: 300, cost: 4.00},
Apple         : { inventory: 450, cost: 4.00}


const fruits = {
    items : [

      ['Mangoes', 50],
      ['Apples', 30]

fruits.items.every((item) => {
  console.log(stocks[item[0]] == item[0]) }) 
// prints false

The[item[0]] is referring to Mangoes and Apples in the const fruits array that might be in the const stocks, hence the need for [0].

Am I correct? :no_mouth:

Yeah, I think you’ve got it. :slight_smile:

.every() applies your test to every item in the array we call it on, so in the fruits array we have two distinct values that will at one point become item:

item number one: ['Mangoes', 50]
item number two: ['Apples', 30]

If we were to just do stocks[item].inventory, we would be attempting to access a property of the stocks object which doesn’t exist - we’re effectively asking for stocks[['Mangoes',50]] or stocks[['Apples', 30]]. We can’t define a property of the object with a name of ['Mangoes', 50], it’s not valid JS.

Hence, we need to pull the name of the fruit that we want from the item array - which is where the 0 index comes in, as the fruit name is in the first position of the fruit item (and the requested amount in the second, index 1).

That help?


This reply of yours confirmed it. This is solved! I really want to thank you for giving me the time to answer my question! It was clear and you laid out every ( no pun intended) detail for people to understand. Your way of explaining was logical yet it doesn’t sound like one. You should be a part of Codecademy’s curriculum team! Hope to get an answer from you again when things get rough on my journey! I can now move on to the async-await lesson. (the code example above was shown as part of an example in the “Chaining Multiple Promises” in Advanced Javascript, Promises topic)

1 Like

No worries, glad you got it sorted and that I could help. :slight_smile: