iterate through array values assigned to object javascript code example
Example 1: iterate through object array javascript
for (var key in array) {
var obj = myArray[key];
// ...
}
Example 2: iterate over array of objects javascript
// Sort an array
const people = [
{ name: "John", age: 21 },
{ name: "Peter", age: 31 },
{ name: "Andrew", age: 29 },
{ name: "Thomas", age: 25 }
];
let sortByAge = people.sort(function (p1, p2) {
return p1.age - p2.age;
});
console.log(sortByAge);