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

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 :: Material-ui add alarm icon 
Javascript :: moment format yyyy-mm-dd 
Javascript :: Round date to future 5min 
Javascript :: Detecting by how much user has scrolled | get how much i scroll in js 
Javascript :: antd tag 
Javascript :: what does document.getelementbyid return 
Javascript :: javascript non-repeating randomize array 
Javascript :: get input js 
Javascript :: json deep dot 
Javascript :: es6 functions 
Javascript :: is vowel javascript 
Javascript :: lastindexof() javascript 
Javascript :: join array of objects javascript 
Javascript :: iban validation regex for all countries 
Javascript :: nextjs query parameter 
Javascript :: reverse js 
Javascript :: js remove last char of string 
Javascript :: html canvas not clearing 
Javascript :: uint8array javascript 
Javascript :: vitejs env 
Javascript :: array of arrays to one array js 
Javascript :: use inline and other styles react native 
Javascript :: mongo connect npm 
Javascript :: upgrading to react 18 
Javascript :: Date object for local time and date 
Javascript :: react props have changed method 
Javascript :: json value types 
Javascript :: context hook 
Javascript :: anonymous function javascript 
Javascript :: javascript check if element has specific child 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =