![]() |
VOOZH | about |
Iteration involves looping through the object's properties one by one. Depending on the method used, you can access and manipulate different levels of properties efficiently. Here are several methods.
A for..in loop iterates over all enumerable properties of an object, including inherited ones. Use hasOwnProperty() to filter only the object’s own properties, and access each value using the key.
book Sherlock Holmes author Arthur Conan Doyle genre Mystery
Syntax:
for (let key in exampleObj) {
if (exampleObj.hasOwnProperty(key)) {
value = exampleObj[key];
console.log(key, value);
}
}Object.entries()returns an array of an object’s own enumerable key-value pairs. Use map() to access each pair, where index 0 is the key and index 1 is the value.
book Sherlock Holmes author Arthur Conan Doyle genre Mystery
Syntax:
Object.entries(exampleObj).map(entry => {
let key = entry[0];
let value = entry[1];
console.log(key, value);
});
object.keys() Method returns an array of keys of the object and forEach() method is an array method that allows you to iterate over each element in the array.
book: Sherlock Holmes author: Arthur Conan Doyle genre: Mystery