xxxxxxxxxx
var people=[
{first_name:"john",last_name:"doe"},
{first_name:"mary",last_name:"beth"}
];
for (let i = 0; i < people.length; i++) {
console.log(people[i].first_name);
}
xxxxxxxxxx
var person={
first_name:"johnny",
last_name: "johnson",
phone:"703-3424-1111"
};
for (var property in person) {
console.log(property,":",person[property]);
}
xxxxxxxxxx
let arr = [object0, object1, object2];
for (let elm of arr) {
console.log(elm);
}
xxxxxxxxxx
const myArray = [{x:100}, {x:200}, {x:300}];
myArray.forEach((element, index, array) => {
console.log(element.x); // 100, 200, 300
console.log(index); // 0, 1, 2
console.log(array); // same myArray object 3 times
});
// Credit Shan Thiru whatever i forgot how to spell his name
xxxxxxxxxx
// Just loop through an array
const myArray = [{x:100}, {x:200}, {x:300}];
myArray.forEach((element, index, array) => {
console.log(element.x); // 100, 200, 300
console.log(index); // 0, 1, 2
console.log(array); // same myArray object 3 times
});
xxxxxxxxxx
var cars = [{name: 'Audi'}, {name: 'BMW'}, {name: 'Ferrari'}, {name: 'Mercedes'}, {name: 'Maserati'}];
for(var i = 0; i < cars.length; i++) {
console.log(cars[i].name);
}
xxxxxxxxxx
// Count the number of each category
const people = [
{name: 'John', group: 'A'},
{name: 'Andrew', group: 'C'},
{name: 'Peter', group: 'A'},
{name: 'James', group: 'B'},
{name: 'Hanna', group: 'A'},
{name: 'Adam', group: 'B'}];
const groupInfo = people.reduce((groups, person) => {
const {A = 0, B = 0, C = 0} = groups;
if (person.group === 'A') {
return {groups, A: A + 1};
} else if (person.group === 'B') {
return {groups, B: B + 1};
} else {
return {groups, C: C + 1};
}
}, {});
console.log(groupInfo); // {A: 3, C: 1, B: 2}
xxxxxxxxxx
yourArray.forEach(function (arrayItem) {
var x = arrayItem.prop1 + 2;
console.log(x);
});
xxxxxxxxxx
var arr = [{id: 1},{id: 2},{id: 3}];
for (var elm of arr) {
console.log(elm);
}