Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript loop through array of objects

let arr = [object0, object1, object2];

for (let elm of arr) {
  console.log(elm);
}
Comment

loop through object in array javascript

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);
}
Comment

iterate through object array javascript

for (var key in array) {
    var obj = myArray[key];
    // ...
}
Comment

iterate over array of objects javascript

// 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
});
Comment

js loop over array of objects extract value

var myArr = [{name: 'rich', secondName: 'james'}, {name: 'brian', secondName: 'chris'}];

var mySecondArr = myArr.map(x => x.name);
console.log(mySecondArr);
Comment

javascript loop through array of objects

yourArray.forEach(function (arrayItem) {
    var x = arrayItem.prop1 + 2;
    console.log(x);
});
Comment

iterate over array of objects javascript

// 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}
Comment

loop object 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
});
Comment

iterate over array of objects javascript

// Transform to a new array

const myArray = [{x:100}, {x:200}, {x:300}];

const newArray= myArray.map(element => element.x);
console.log(newArray); // [100, 200, 300]
Comment

iterate over array of objects javascript

// Check if any of the elements in an array pass a test

const people = [
    {name: 'John', age: 23}, 
    {name: 'Andrew', age: 3}, 
    {name: 'Peter', age: 8}, 
    {name: 'Hanna', age: 14}, 
    {name: 'Adam', age: 37}];

const anyAdult = people.some(person => person.age >= 18);
console.log(anyAdult); // true
Comment

javascript loop through array of objects

var arr = [{id: 1},{id: 2},{id: 3}];

for (var elm of arr) {
  console.log(elm);
}
Comment

javascript for loop array of objects

var array = ["e", 5, "cool", 100];

for (let i = 0; i < array.length; i++) {
	console.log(array[i]);
}

// This is a common method used to loop through elements in arrays.
// You can use this to change elements, read them, and edit them
Comment

iterate over array of objects javascript

// Find an element in an array

const people = [ {name: "john", age:23},
                {name: "john", age:43},
                {name: "jim", age:101},
                {name: "bob", age:67} ];

const john = people.find(person => person.name === 'john');
console.log(john);
Comment

iterate over array of objects javascript

// Retrieve a subset of an array based on particular criteria

const myArray = [{x:100}, {x:200}, {x:300}];

const newArray = myArray.filter(element => element.x > 250);
console.log(newArray); // [{x:300}] 
Comment

array-object loop

const data = [
  { name: 'John', age: 30, group: 'A' },
  { name: 'Mary', age: 25, group: 'B' },
  { name: 'Mike', age: 20, group: 'A' },
  { name: 'Jane', age: 15, group: 'C' },
  { name: 'Peter', age: 25, group: 'B' }
];

// for loop
for (let i = 0; i < data.length; i++) {
  console.log(data[i]);
}
Comment

array-object loop

const data = [
  { name: 'John', age: 30, group: 'A' },
  { name: 'Mary', age: 25, group: 'B' },
  { name: 'Mike', age: 20, group: 'A' },
  { name: 'Jane', age: 15, group: 'C' },
  { name: 'Peter', age: 25, group: 'B' }
];

// for loop
for (let i = 0; i < data.length; i++) {
  console.log(data[i]);
}
Comment

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);
Comment

array-object loop

const data = [
  { name: 'John', age: 30, group: 'A' },
  { name: 'Mary', age: 25, group: 'B' },
  { name: 'Mike', age: 20, group: 'A' },
  { name: 'Jane', age: 15, group: 'C' },
  { name: 'Peter', age: 25, group: 'B' }
];

// for...in loop
for (const index in data) {
  console.log(index, data[index]);
}
Comment

array-object loop

const data = [
  { name: 'John', age: 30, group: 'A' },
  { name: 'Mary', age: 25, group: 'B' },
  { name: 'Mike', age: 20, group: 'A' },
  { name: 'Jane', age: 15, group: 'C' },
  { name: 'Peter', age: 25, group: 'B' }
];

// forEach loop
data.forEach(function(item, index) {
  console.log(item, index);
});
Comment

iterate over array of objects javascript

// Sum up a particular property, and calculate its average

const myArray = [{x:100}, {x:200}, {x:300}];

const sum = myArray.map(element => element.x).reduce((a, b) => a + b, 0);
console.log(sum); // 600 = 0 + 100 + 200 + 300

const average = sum / myArray.length;
console.log(average); // 200
Comment

javascript loop through array of objects

//Only to be used if you need the numbers
var array = new Array(item1,item2,item3)

for(i=0;i<array.length;i++){
  if(i==2){
    console.log(array[i])
  }
}
Comment

for loop javascript array of objects

 " Testing Grepper 
Comment

iterate over array of objects javascript

// Create a new array based on the original but without modifying it

const myArray = [{x:100}, {x:200}, {x:300}];

const newArray= myArray.map(element => {
    return {
        ...element,
        x: element.x * 2
    };
});

console.log(myArray); // [100, 200, 300]
console.log(newArray); // [200, 400, 600]
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript get users location 
Javascript :: javascript get bit 
Javascript :: js scrollto 
Javascript :: find particular object from array in js 
Javascript :: disable mouse right click javascript 
Javascript :: add class on javascript onclick function 
Javascript :: video js toggle play pause 
Javascript :: angular MatDialogRef spec 
Javascript :: how to set background automatically with my screen height 
Javascript :: fill array with random numbers javascript 
Javascript :: find min and max date in array javascript 
Javascript :: Adblock detection in website using javascript 
Javascript :: node js request async await 
Javascript :: how to loop through an object using lodash 
Javascript :: jquery calc height based on width 
Javascript :: Format number thousands k javascript 
Javascript :: hnazmul 
Javascript :: check if there is data in localstorage 
Javascript :: scrollview scroll to bottom react native 
Javascript :: how to access xpath in js 
Javascript :: react map key increment 
Javascript :: angular declare variable in a file 
Javascript :: jquery event source 
Javascript :: change mouse highlight color js 
Javascript :: charcodeat vs codepointat 
Javascript :: javascript array value dom 
Javascript :: redirect to homepage javascript 
Javascript :: sort array of objects javascript 
Javascript :: json server 
Javascript :: do not trigger useeffect on start 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =