Home > Enterprise >  How to loop over an object inside of an object
How to loop over an object inside of an object

Time:12-08

The server gives the following response:

SomeObject:{
    Object1:{
        id: 123456789,
        name: "Foo"
    },
    Object2:{
        id: 123456789,
        name: "Bar"
    }
}

Is it possible to loop over SomeObject and display both the id and name of Object1/Object2? Searching for this mostly lead to using Object.keys(SomeObject).map however those are use to get the string of Object1/Object2.

CodePudding user response:

const SomeObject = { Object1: { id: 123456789, name: "Foo" }, Object2:{ id: 123456789, name: "Bar" } };

const res = 
  Object.values(SomeObject)
  .forEach(({ id, name }) => console.log(id, name));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

One way is with Object.values then forEach.

SomeObject = {
    Object1:{
        id: 123456789,
        name: "Foo"
    },
    Object2:{
        id: 123456789,
        name: "Bar"
    }
}

Object.values(SomeObject).forEach(function (value) {
     console.log(value.id);
     //value.name
});

CodePudding user response:

You can use for-in loops to iterate objects

for ( let index in SomeObject ) {
  for ( let sub_index in SomeObject[ index ] ) {

    console.log( 'index', index, 'SomeObject[ index ]', SomeObject[ index ] )
    console.log( 'sub_index', sub_index, 'SomeObject[ index ][ sub_index ]', SomeObject[ index ][ sub_index ] )

  }
}

CodePudding user response:

Another approach utilizing two forin loop.

    obj = {
        SomeObject: {
            Object1: {
                id: 123456789,
                name: "Foo"
            },
            Object2: {
                id: 123456789,
                name: "Bar"
            }
        }
    }

    for (const key in obj) {
        if (Object.hasOwnProperty.call(obj, key)) {
            const
                obj2 = obj[key];
            for (const key2 in obj2) {
                if (Object.hasOwnProperty.call(obj2, key2)) {
                    inner_obj2 = obj2[key2];
                    console.log(inner_obj2.id, inner_obj2.name);
                }
            }
        }
    }
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

So if you have an object with an unknown amount of properties but you know all of those properties would be objects you can do this ...

let obj = {
    prop1: {
    id: 1,
    someProp: { subId: 's1' }
  },
  propA: {
    id: 2,
    someProp: { subId: 's2' }
  },
  prop3: {
    id: 3,
    someProp: { subId: 's3' }
  }
}

/**
* Recursively iterate an object and its properties.
* @param {object} theObj the object to be iterated
*/
function iterate(theObj) {
    for (const key of Object.keys(theObj)) {
        console.log(`type of ${key} = ${typeof theObj[key]}`);
        // do something
        if (typeof theObj[key] == 'object') {
            iterate(theObj[key]);
        }
    }
}

iterate(obj);

// output
// type of prop1 = object
// type of id = number
// type of someProp = object
// type of subId = string
// type of propA = object
// type of id = number
// type of someProp = object
// type of subId = string
// type of prop3 = object
// type of id = number
// type of someProp = object
// type of subId = string

It's by no means perfect but it works for objects and arrays. Even tried it with this piece of madness.

let arr = [
    0,
    'test',
    {
        id: 1,
        subarr: [
            32,
            45,
            76
        ],
        subObj: {
            key:'foo',
            lock: 'bar'
        }
    }
]

iterate(arr);

// output
// type of 0 = number
// type of 1 = string
// type of 2 = object
// type of id = number
// type of subarr = object
// type of 0 = number
// type of 1 = number
// type of 2 = number
// type of subObj = object
// type of key = string
// type of lock = string
  • Related