Yes, you can use the Object.keys()
and Object.values()
methods to traverse through the properties of embedded objects without using nested for-loops. Here is an example:
const obj = {
a: 1,
b: {
c: 2,
d: 3
}
};
// Get the keys of the outer object
const keys1 = Object.keys(obj);
// Loop through the keys and check if the value is an object
keys1.forEach(key1 => {
const value1 = obj[key1];
if (typeof value1 === 'object' && value1 !== null) {
// Get the keys of the inner object
const keys2 = Object.keys(value1);
// Loop through the keys and get the values
keys2.forEach(key2 => {
const value2 = value1[key2];
console.log(key1, key2, value2);
});
}
});
Output:
b c 2
b d 3
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-11 09:51:52 +0000
Seen: 8 times
Last updated: Jul 11 '23
How to provide parameters to machine learning model objects?
Is it impossible to delete any objects provided by ActiveRecord_Associations_CollectionProxy?
What is the technique for creating a mapping of an array containing various objects and values?
How can an array of objects be initialized in C# without implementing a loop?
Is it possible to retrieve objects as a list in jq in order to access them by index?
How to query an array of objects with JSONB in Postgres?
How can the clipping of objects in matplotlib 3D be modified?
In Laravel, what is the process for comparing two objects and obtaining distinct columns?