How to use the for..in loop In Javascript
The for..in loop is a type of loop that is used in JavaScript and this can be used pretty well in accessing elements of the array. By this method, you can access both the whole elements and the properties inside the objects.
Syntax:
for (var key in arrayName) {
console.log(arrayName[key].name);
//Or we can access the specific properties using
console.log(arrayName[arrayIndex].propertyName);
}
Example: The code below demonstrates how we can use the for..in the loop to access the elements of the array of objects:
Javascript
// Array of objects let objArr = [ { name: 'john' , age: 12, gender: 'male' }, { name: 'jane' , age: 15, gender: 'female' }, { name: 'julie' , age: 20, gender: 'trans' } ]; console.log( "Accessing the values using the for..in loop:" ) for (let key in objArr) { // Console logs all the // values in the objArr Array: console.log(objArr[key]); } |
Output
Accessing the values using the for..in loop: { name: 'john', age: 12, gender: 'male' } { name: 'jane', age: 15, gender: 'female' } { name: 'julie', age: 20, gender: 'trans' }
How to Access Array of Objects in JavaScript ?
In this article, we are going to learn how we Access an Array of Objects in JavaScript. An array of objects in JavasScript is a collection of elements that individually hold a number of properties and values.
The approaches to access the array of objects in JavaScript are:
Table of Content
- Using the Brackets notation
- Using the DOT notation
- Using the for..in loop
- Using forEach Loop
- Using map() method
- Using filter() method
Contact Us