js loop on objects code example

Example 1: javascript iterate object

const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.entries(obj)); // => [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]

Object.entries(obj).forEach((key, value) => {
  console.log(key + ' ' + value);
});

Example 2: loop an object in javascript

var obj = { first: "John", last: "Doe" };

Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
});

Example 3: forin js

let panier = ['fraise', 'banane', 'poire'];

for (const fruit in panier) {
    console.log(panier[fruit]);
}

Example 4: javascript loop through object

var person = {"name":"bob", age:45};

for (var property in person) {
   console.log(person[property]);
}