In JavaScript, you can use the for...in loop to iterate over the properties of an object and the for...of loop to iterate over the elements of an array.
Here's an example of how to use for...in to loop through the properties of an object:
const person = {
name: 'John',
age: 30,
city: 'New York'
};
for (let prop in person) {
console.log(`${prop}: ${person[prop]}`);
}
Output:
vbnet
Copy code
name: John
age: 30
city: New York
Here's an example of how to use for...of to loop through the elements of an array:
const numbers = [1, 2, 3, 4, 5];
for (let num of numbers) {
console.log(num);
}
Output:
1
2
3
4
5
To loop through the entries of an array, you can use the entries() method to get an iterator that returns the index and value of each element. Here's an example:
const fruits = ['apple', 'banana', 'orange'];
for (let [index, value] of fruits.entries()) {
console.log(`Index: ${index}, Value: ${value}`);
}
Output:
Index: 0, Value: apple
Index: 1, Value: banana
Index: 2, Value: orange
==================================================
To loop through the entries of an object in JavaScript, you can use the Object.entries() method. This method returns an array of the object's own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a for...in loop. You can then use the forEach() method to iterate through the array.
Here's an example of how to use Object.entries() and forEach() to loop through the entries of an object:
const person = {
name: 'John',
age: 30,
city: 'New York'
};
Object.entries(person).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});
Output:
name: John
age: 30
city: New York
In the example above, we first call Object.entries(person) to get an array of [key, value] pairs for the person object. We then use the forEach() method to loop through each entry in the array. The destructuring assignment syntax ([key, value]) is used to extract the key and value of each entry, which are then logged to the console.
Note that the Object.entries() method is only available in modern browsers and may not be supported in older browsers.
In JavaScript, you can use a for...of loop to iterate over an array, and the for...in loop to iterate over an object's enumerable properties. To loop through the entries of an object, you can use the Object.entries() method to get an array of the object's own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a for...in loop.
Here's an example of how to use a for...of loop and Object.entries() to loop through the entries of an object:
const person = {
name: 'John',
age: 30,
city: 'New York'
};
for (let [key, value] of Object.entries(person)) {
console.log(`${key}: ${value}`);
}
Output:
name: John
age: 30
city: New York
In the example above, we use the for...of loop to loop through each entry in the array returned by Object.entries(person). The destructuring assignment syntax [key, value] is used to extract the key and value of each entry, which are then logged to the console.
Note that the Object.entries() method is only available in modern browsers and may not be supported in older browsers.
=============================================
OUTPUT
Top comments (0)