#looping over object

1 messages · Page 1 of 1 (latest)

wheat drum
#

heyh guys, it's a stupid question I know, but I'm kinda confused, probably cause I'm bonking my head around this for way too long. How can I loop over this object? Like, I can get to the first two ids and get the title, but then I wouldn't know how to go deeper.

The number of new nested objects will vary as you can see, and the names are... well, not random but, words that the user will input

(pls ping me)

const data = {
  "4214023": {
    "title": "Nighthaven Labs"
  },
  "6869036": {
    "title": "Villa",
    "Ela": {
      "2435782": {
        "killsInput": "19",
        "deathsInput": "12"
      },
      "3922306": {
        "killsInput": "20",
        "deathsInput": "17"
      }
    },
    "Alibi": {
      "2850831": {
        "killsInput": "20",
        "deathsInput": "17"
      }
    }
  }
};
hoary fiber
#
for (const id of Object.keys(data)) {
    const item = data[id];
    console.log("Item title", item.title);
}

@wheat drum ?

blissful raptor
#

extend on rob's answer by checking if they key holds a obj
smn like

if (typeof item === "object" && item !== null) {
  // continue traversing;
}

dont do it recursively if its too deep

wheat drum
# hoary fiber ```js for (const id of Object.keys(data)) { const item = data[id]; conso...

ello, yeah this wuold indeed work, but then I wouldn't rlly know how to go deeper and loop through them, specially since I don't know how they are going to be called. For example, there are ids for different matches and I wouldn't know how to loop over those since I can't know their names.

Basically, how can I go in "item.Ela", and then ever deeper in item.Ela[5514543]

hoary fiber
#

Recursion

wheat drum
#

something like this?

function iterateNestedObjects(obj, path = []) {
    for (let key in obj) {
      const newPath = [...path, key]; // Add current key to the path
      if (typeof obj[key] === "object") {
        iterateNestedObjects(obj[key], newPath); // Recursive call with updated path
      } else {
        const recursiveString = newPath.join(" > ") + " Value: " + obj[key];
        // console.log('recursiveString: ', recursiveString);
hoary fiber
#

Yes

wheat drum
# hoary fiber Yes

oh ok, so I pretty much have it but eh, I'm still confused on how I'm supposed to loop over it, like, all the keys are gonna change and I won't know their names, so I basically don't know how to move through the object, like imagine this:

title.matchOne.character.kills
it's extremely easy this way, but when you don't know all those names... not rlly sure how I should do it

hoary fiber
#

Object.keys()

wheat drum
#

yeah but how do I go deeper then?

hoary fiber
#
function recurseObj(value, path=[]) {
    for (const id of Object.keys(data)) {
        const item = data[id];
        if (typeof item === "object") {
             return recurseObj(value, [...path, key]);
        }

        console.log("Path: ", path, ", value: ", item);
    }
}```
#

or something

wheat drum
#

idk I don't quite understand it