loop through array and create object javascript code example
Example 1: javascript loop through object example
var person={
first_name:"johnny",
last_name: "johnson",
phone:"703-3424-1111"
};
for (var property in person) {
console.log(property,":",person[property]);
}
Example 2: javascript loop through array of objects es6
const people = [
{id: 100, name: 'Vikash'},
{id: 101, name: 'Sugam'},
{id: 102, name: 'Ashish'}
];
for (let persone of people) {
console.log(persone.id + ': ' + persone.name);
}
people.forEach(person => {
console.log(persone.id + ': ' + persone.name);
});
people.forEach((person, index) => {
console.log(index + ': ' + persone.name);
});
Example 3: iterate over array of objects javascript
const myArray = [{x:100}, {x:200}, {x:300}];
const newArray= myArray.map(element => element.x);
console.log(newArray);
Example 4: javascript loop through array of objects
var array = new Array(item1,item2,item3)
for(i=0;i<array.length;i++){
if(i==2){
console.log(array[i])
}
}