Welcome, JavaScript explorers! This chapter equips you with the knowledge to traverse the fascinating landscapes of objects – their properties. We'll delve into various techniques to loop through object properties, unlocking their treasures and extracting valuable data.
for...in
Loopfor...in
loop is a fundamental approach to iterating over enumerable properties (properties that can be looped through) in an object.
const person = {
firstName: "Alice",
lastName: "Smith",
age: 30
};
for (const key in person) {
console.log(key, person[key]);
}
/* Output:
firstName Alice
lastName Smith
age 30
*/
person
with three properties.for...in
loop iterates through each key (property name) in the person
object.key
holds the current property name, and we use person[key]
to access the corresponding value.Object.keys()
and for...of
LoopObject.keys()
method to create an array containing only the enumerable property names of the object. Then, a for...of
loop iterates through this array.
const person = {
firstName: "Alice",
lastName: "Smith",
age: 30
};
for (const key in person) {
console.log(key, person[key]);
}
/* Output:
firstName Alice
lastName Smith
age 30
*/
person
object.Object.keys(person)
returns an array named keys
containing only the property names ("firstName"
, "lastName"
, "age"
).for...of
loop iterates through each element (property name) in the keys
array.person
object and log them.hasOwnProperty()
MethodhasOwnProperty()
method helps determine if a property is directly defined on the object itself (not inherited from its prototype). This can be useful when you only want to iterate over the object’s own properties.
const person = {
firstName: "Alice"
};
person.__proto__.lastName = "Smith"; // Add lastName to prototype
for (const key in person) {
if (person.hasOwnProperty(key)) {
console.log(key, person[key]);
}
}
/* Output:
firstName Alice
*/
person
object with firstName
.lastName
property to the prototype chain using person.__proto__.lastName
.for...in
loop iterates through all properties (including inherited ones).hasOwnProperty(key)
checks if key
("lastName"
in this case) is a property directly on person
. Since it’s inherited, it returns false
.true
from hasOwnProperty
, ensuring we iterate through the object’s own properties.Object.entries()
and Destructuring Object.entries()
returns an array of arrays, where each inner array contains a key-value pair. Destructuring allows us to unpack these pairs into variables.
const person = {
firstName: "Alice",
lastName: "Smith"
};
for (const [key, value] of Object.entries(person)) {
console.log(key, value);
}
/* Output:
firstName Alice
lastName Smith
*/
person
object.Object.entries(person)
returns an array of arrays. Each inner array contains a key-value pair like ["firstName", "Alice"]
.for...of
loop iterates through these inner arrays.[key, value]
unpacks each inner array, assigning the key to key
and the value to value
within the loop.key
and value
for clarity.Object.values()
with a for...of
loop. Object.values()
returns an array containing only the values of the enumerable properties.
const person = {
firstName: "Alice",
lastName: "Smith",
age: 30
};
for (const value of Object.values(person)) {
console.log(value);
}
/* Output:
Alice
Smith
30
*/
person
object with three properties: firstName
, lastName
, and age
.Object.values()
method to create an array named values
. This array contains only the values of the enumerable properties in the person
object. In this case, values
will be ["Alice", "Smith", 30]
.for...of
loop iterates through each element (value) in the values
array.value
) to the console.Mastering object iteration unlocks JavaScript's object treasures. You can traverse properties with for...in loops, leverage Object.keys() for cleaner access, and utilize Object.values() to focus solely on data. Choose the method that best suits your data manipulation needs! Happy coding !❤️