Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my code which loops through objects in an array: 

for (var j = 0; j < myArray.length; j++){

console.log(myArray[j]);

}

The problem is it displays only the first object. The below code is to access a particular Object1.x in the array by looping. 

for (var j = 0; j < myArray.length; j++){

console.log(myArray[j.x]);

}

It returns with the error, “undefined”. Can anyone tell me how to do this? 

1 Answer

0 votes
by (19.7k points)

You can use “Array.forEach() built-in function. Check the below code: 

yourArray.forEach(function (arrayItem) {

    var x = arrayItem.prop1 + 2;

    console.log(x);

});

Interested in Java? Check out this Java tutorial by Intellipaat.

Related questions

Browse Categories

...