How to loop through an array of objects in JavaScript

PHOTO EMBED

Fri Sep 17 2021 19:58:29 GMT+0000 (Coordinated Universal Time)

Saved by @mesfint #javascript

const mobiles = [
    {
        brand: 'Samsung',
        model: 'Galaxy Note 9'
    },
    {
        brand: 'Google',
        model: 'Pixel 3'
    },
    {
        brand: 'Apple',
        model: 'iPhone X'
    }
];

mobiles.forEach(mobile => {
    for (let key in mobile) {
        console.log(`${key}: ${mobile[key]}`);
    }
});
content_copyCOPY

To iterate through an array of objects in JavaScript, you can use the forEach() method aong with the for...in loop.

https://attacomsian.com/blog/javascript-iterate-array-of-objects