Don't understand the order of properties in the "totalCountPerProduct" Object of the "addItem" method in the "Learn basic OOP by building a shopping cart"

so I just tried to console.log some value after calling the addItem object to understand more how it work and I just saw that the order of properties in the “totalCountPerProduct” is kinda different as what I expect.


I figured that since I’d added the item with the id “4” first, and then the one with the id “8” and the last one is “2” so in the object storing the value of products, the product with the id “4” must have been added to the object as the first property. But once I console.log the object containing those value of products per id. I saw the product with id “2” added to the object first, and then “4”, and then “8”. But taking a look at how the amount of products counted by looping over the items array and then adding 1 to the property which was equaled to the id of the product when it was more than 1, I just saw that the product having the id “4” was the first item in the array so I thought in the object storing values of numbers of products, the one with “4” would be looped over first and then getting added to the object first . I found it kind of strange because it was so different as how I understood. I could’ve got it wrong and I’m so confuse right now. So can anyone give me an explanation about how it actually works in this case? Thanks!!
link to the step: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/learn-basic-oop-by-building-a-shopping-cart/step-54

try looking at the code executed with this tool Online JavaScript Compiler, Visual Debugger, and AI Tutor - Learn JavaScript programming by visualizing code, maybe it helps if you visualize it

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.