object javascript loop code example

Example 1: javascript loop through object

Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);

Example 2: loop through object js

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

//
//	Visit non-inherited enumerable keys
//
Object.keys(obj).forEach(function(key) {

  console.log(key, obj[key]);

});

Example 3: loop in object javascript

var person = {"name":"Rasel", age:26};

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

Example 4: forin js

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

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

Example 5: javascript looping through object

for (const [fruit, count] of entries) {
  console.log(`There are ${count} ${fruit}s`)
}

// Result
// There are 28 apples
// There are 17 oranges
// There are 54 pears