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.
const SomeObject = { Object1: { id: 123456789, name: "Foo" }, Object2:{ id: 123456789, name: "Bar" } };
const res =
Object.values(SomeObject)
.forEach(({ id, name }) => console.log(id, name));
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
});
The key to solving this is the Object.values() method, the result of which can be iterated using a for..of or .forEach() loop:
const SomeObject = { Object1: { id: 123456789, name: "Foo" }, Object2:{ id: 123456789, name: "Bar" } };
for( let {id,name} of Object.values( SomeObject ) ) {
console.log( id, name );
}