Accessing nested objects like 2D arrays

So I’m not sure whether I have a fundamental misunderstanding of how objects work, but I’m trying to write a program which requires accessing nested objects, however it’s not a simple case of accessing the object via the object name. For example, if I have:

const itemsList = {
  item1: {
    ID: 0,
    itemName: "Item one",
  item2: {
    ID: 1,
    itemName: "Item two",

In my code, I want to access a particular item using a number which corresponds to the ID attribute of my objects. I don’t have access to the object names so if this was like a 2D array, I could do something like:

let num = getIDNumber(); // returns 1, for example
for (let i = 0; i < itemsList.length; i++){
  if (itemsList[i][0] == num){
    currentName = itemsList[i][1];

However this does not work. Am I misunderstanding how objects work and do I need to use something else to store this data? I have about 50 entries, each with 4 properties so I really don’t want to use arrays.

I think the problem is you have itemsList[i][1], but the code has itemsList.item1. The itemsList does not have a property that corresponds to the simple [i] that you’re passing.

You should use an array of objects instead of an object of objects.

itemsList = [];
 itemName: 'name',
 itemCost: cost
``` etc.
1 Like

objects are not indexed, to access any value you must have the corresponding key. to iterate over object without going too in depth you would use the for ... in loop structure. see MDN doc

then to access the nested part you need to do something similar. if you do obj[i][0], then javascript is going to try to find the the property literally named ‘0’ in obj[i] which from your json structure doesn’t exist. if you want to access the ID, you would do obj[key]['ID']

sidenote, why not arrays? the way you are trying to look up certain things would work with arrays. Also I recommend going over the parts of the curriculum covering Javascript objects again as refresher. Good luck!

1 Like

I was just giving an example of what I wanted it to do, but yes unfortunately they can’t behave as indexes so I can’t do that. And I didn’t realize you could make arrays of objects, but that makes sense. I’ll have to keep that in mind for the future, thanks.

Thanks, I ended up getting this working. The solution I got was:

for (const objectName in itemsList){
  if (itemsList[objectName]['ID'] == num){
    currentName = itemsList[objectName]['itemName'];

I want to both get into the habit of using objects and they really help make my data more clear, And to be completely honest I haven’t touched the Object Oriented Programming section of the Javascript course yet, I thought I’d be able to manage with the basics for this project. Fortunately it wasn’t that bad but I definitely have a lot more to learn, thanks!

The structure you have is fine, and if you can’t change it then it’s easy enough to deal with.

You need the key, so

Function extractItem(Argument items, Argument targetId):
  Convert "items" to an array using Object.entries
  This produces an array of [key, value] tuples:
    [[key, {ID, itemName}], [key, {ID, itemName], ...]
  Use "find" on this array to locate the item whose ID matches "targetId"

  If "find" returns a result:
    The result will be: [key, {ID, itemName}]
    Return items[key]
    Fail, there is nothing in "items" with "targetId"

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