showing results for - "iterate over array of objects javascript"
Jeffry
11 Nov 2016
1// Just loop through an array
2
3const myArray = [{x:100}, {x:200}, {x:300}];
4
5myArray.forEach((element, index, array) => {
6    console.log(element.x); // 100, 200, 300
7    console.log(index); // 0, 1, 2
8    console.log(array); // same myArray object 3 times
9});
10
Nora
19 Mar 2020
1// Count the number of each category
2
3const people = [
4    {name: 'John', group: 'A'}, 
5    {name: 'Andrew', group: 'C'}, 
6    {name: 'Peter', group: 'A'}, 
7    {name: 'James', group: 'B'}, 
8    {name: 'Hanna', group: 'A'}, 
9    {name: 'Adam', group: 'B'}];
10
11const groupInfo = people.reduce((groups, person) => {
12    const {A = 0, B = 0, C = 0} = groups;
13    if (person.group === 'A') {
14        return {...groups, A: A + 1};
15    } else if (person.group === 'B') {
16        return {...groups, B: B + 1};
17    } else {
18        return {...groups, C: C + 1};
19    }
20}, {});
21
22console.log(groupInfo); // {A: 3, C: 1, B: 2}
23
Liah
16 Sep 2016
1// Transform to a new array
2
3const myArray = [{x:100}, {x:200}, {x:300}];
4
5const newArray= myArray.map(element => element.x);
6console.log(newArray); // [100, 200, 300]
7
Valerio
01 Apr 2020
1// Find an element in an array
2
3const people = [ {name: "john", age:23},
4                {name: "john", age:43},
5                {name: "jim", age:101},
6                {name: "bob", age:67} ];
7
8const john = people.find(person => person.name === 'john');
9console.log(john);
10
Fantine
12 Jun 2016
1// Create a new array based on the original but without modifying it
2
3const myArray = [{x:100}, {x:200}, {x:300}];
4
5const newArray= myArray.map(element => {
6    return {
7        ...element,
8        x: element.x * 2
9    };
10});
11
12console.log(myArray); // [100, 200, 300]
13console.log(newArray); // [200, 400, 600]
14
Alix
29 Mar 2017
1// Retrieve a subset of an array based on particular criteria
2
3const myArray = [{x:100}, {x:200}, {x:300}];
4
5const newArray = myArray.filter(element => element.x > 250);
6console.log(newArray); // [{x:300}] 
7
Nehemiah
12 Mar 2020
1// Sum up a particular property, and calculate its average
2
3const myArray = [{x:100}, {x:200}, {x:300}];
4
5const sum = myArray.map(element => element.x).reduce((a, b) => a + b, 0);
6console.log(sum); // 600 = 0 + 100 + 200 + 300
7
8const average = sum / myArray.length;
9console.log(average); // 200
10
Simón
28 Jan 2017
1// Check if any of the elements in an array pass a test
2
3const people = [
4    {name: 'John', age: 23}, 
5    {name: 'Andrew', age: 3}, 
6    {name: 'Peter', age: 8}, 
7    {name: 'Hanna', age: 14}, 
8    {name: 'Adam', age: 37}];
9
10const anyAdult = people.some(person => person.age >= 18);
11console.log(anyAdult); // true
12
Clodagh
26 May 2020
1// Sort an array
2
3const people = [
4  { name: "John", age: 21 },
5  { name: "Peter", age: 31 },
6  { name: "Andrew", age: 29 },
7  { name: "Thomas", age: 25 }
8];
9
10let sortByAge = people.sort(function (p1, p2) {
11  return p1.age - p2.age;
12});
13
14console.log(sortByAge);
15