Iterating through an array of objects using for each or for in

Chidexebere

Please how do one iterate over a array of object like using "for each" or "for in or is there another way

{
    "cards": [
        {
            "des": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ut diam ac felis placerat consequat in vitae justo. Curabitur porta et dolor ac. Morbi quis elementum ipsum",
            "cardName": "Aenean nec sem vestibulum",
            "imgSrc": "Layer1.png",
            "id": 1
        },
        {
            "des": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ut diam ac felis placerat consequat in vitae justo. Curabitur porta et dolor ac. Morbi quis elementum ipsum",
            "cardName": "Aenean nec sem vestibulum",
            "imgSrc": "tom-jagger.jpg",
            "id": 2
        },
        {
            "des": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ut diam ac felis placerat consequat in vitae justo. Curabitur porta et dolor ac. Morbi quis elementum ipsum",
            "cardName": "Aenean nec sem vestibulum",
            "imgSrc": "Layer3.png",
            "id": 3
        }
    ]
}

i need the output to be each object in the array Also lets say i want to do the iteration using the "id" parameter. how do i do this

Dacre Denny

You have a number of options availbe to you for iterating (looping) through data like this. The more common methods would be:

Iteration via Array#forEach():

data.cards.forEach(function(card) {
  console.log(card);
});

Iteration via an iterator using the for-of construct:

for(const card of data.cards) {
  console.log(card);
};

Iteration via an iterator using the for-in construct:

for(const key in data.cards) {
  const value = data.cards[key];
  console.log(value);
};

Here is a working snippet showing these in action:

var data = {
    "cards": [
        {
            "des": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ut diam ac felis placerat consequat in vitae justo. Curabitur porta et dolor ac. Morbi quis elementum ipsum",
            "cardName": "Aenean nec sem vestibulum",
            "imgSrc": "Layer1.png",
            "id": 1
        },
        {
            "des": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ut diam ac felis placerat consequat in vitae justo. Curabitur porta et dolor ac. Morbi quis elementum ipsum",
            "cardName": "Aenean nec sem vestibulum",
            "imgSrc": "tom-jagger.jpg",
            "id": 2
        },
        {
            "des": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ut diam ac felis placerat consequat in vitae justo. Curabitur porta et dolor ac. Morbi quis elementum ipsum",
            "cardName": "Aenean nec sem vestibulum",
            "imgSrc": "Layer3.png",
            "id": 3
        }
    ]
};

// Array.forEach method
data.cards.forEach(function(card) {
  console.log(card);
});

// for-of loop
for(const card of data.cards) {
  console.log(card);
};

// for-in loop
for(const key in data.cards) {
  const value = data.cards[key];
  console.log(value);
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Iterating through object Where Value is Array of Objects and return one value from each Objects of Array of Objects

Iterating through an array of Objects - Javascript

Using a pug each loop through an array of objects

iterating through array of nested objects with javascript/lodash

Create array of objects by iterating through an observable

Merging some objects by iterating through an array

Iterating through an array of Class::Struct objects

TS error on iterating through an array of objects

NullPointerException when iterating through an array of objects

Iterating through an array of objects to filter specific data

Iterating through an array of objects C++

trouble iterating through array containing objects in java

iterating through JSON objects in groovy using maps

Why do 'for' and 'for_each' result in different functions being generated by iterating through array elements using lambdas?

Iterating through nested objects to get single property from each

(Conceptual) Iterating through an array of ints using a pointer

Iterating through vue array using index

Iterating through an array and searching for each item in the array in a file

Iterating through an objects values

Iterating through lots of objects

Iterating through javascript objects

Iterating through JSON objects

javascript iterating array of objects using checkboxes

Iterating an array of objects using for of loop in javascript

Iterating json array objects using node js

Iterating over an array of objects using DataWeave

Iterating two array of diferents objects using linq

Iterating through array of objects from your angular 4 firebase database

iterating through an array of objects and displaying the items [REACT JS]