Open In App

How to Access Array of Objects in JavaScript ?

Last Updated : 30 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

access-array-of-objects

How to Access an Array of Objects in JavaScript?

The approaches to access the array of objects in JavaScript are:

Using the Brackets notation

Using bracket notation and the object’s desired index, you can access an object in an array. In type, the whole object is accessed and only one of the properties of a specific object can’t be accessed.

Syntax:

arrayName[arrayIndex]

Example: The code below demonstrates how we can use the brackets notation 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("First Object in the Array using the [] notation:")
console.log(objArr[0]);


Output

First Object in the Array using the [] notation:
{ name: 'john', age: 12, gender: 'male' }

Using the DOT notation

This method can’t be used directly to access the array of objects but we have to use it together with the bracket notation. With this method, we can access any specific property of a particular object in the array of objects but not the whole object.

Syntax:

arrayName[arrayIndex].propertyName

Example: The code below demonstrates how we can use the DOT notation along with the brackets notation 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 value using the [] and DOT notations:")
console.log(objArr[1].gender);


Output

Accessing the value using the [] and DOT notations:
female

Using the for..in loop

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' }

Using forEach Loop

The forEach loop allows you to loop through an array of objects and access each one individually. By this method too, you can access both the whole elements and the properties inside the objects.

Syntax:

arrayName.forEach(function(item) {
console.log(item);
});

Example: The code below demonstrates how we can use the forEach 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 arrayusing the forEach loop:");
objArr.forEach(function (item) {
    console.log(item);
});


Output

Accessing the arrayusing the forEach loop:
{ name: 'john', age: 12, gender: 'male' }
{ name: 'jane', age: 15, gender: 'female' }
{ name: 'julie', age: 20, gender: 'trans' }

Using map() method

The map() function in JavaScript allows you to access an array of objects and change it into another array. The map() function calls a supplied function on each element of the original array, producing the results in a new array.

Syntax:

arrayName.map((item) => {
console.log(item);
});

Example: The code below demonstrates how we can use the forEach 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 Array using the forEach loop:")
objArr.map((item) => {
    console.log(item);
});


Output

Accessing the Array using the forEach loop:
{ name: 'john', age: 12, gender: 'male' }
{ name: 'jane', age: 15, gender: 'female' }
{ name: 'julie', age: 20, gender: 'trans' }

Using filter() method

You may access an array of items and create a new array in JavaScript by using the map() function. The map() function calls a given function on each element of the original array, creating a new array with the results.

Syntax:

arrayName.filter(function(item) {
console.log(item);
});

Example: The code below demonstrates how we can use the filter function to access the elements of the array of objects. It also shows how we can get a specific object from the array of objects using the clause in the filter function:

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 Array using the forEach loop:")
objArr.filter(function (item) {
    console.log(item);
});
console.log("Using the Filer method to acces value")
const search = objArr.filter(function (item) {
    return item.name === 'jane';
});
console.log(search);


Output

Accessing the Array using the forEach loop:
{ name: 'john', age: 12, gender: 'male' }
{ name: 'jane', age: 15, gender: 'female' }
{ name: 'julie', age: 20, gender: 'trans' }
Using the Filer method to ...


Similar Reads

Filter Array of Objects with Another Array of Objects in JavaScript
Filtering an array of objects with another array in JavaScript involves comparing and including objects based on specific criteria. Below are the approaches to filter an array of objects with another array of objects in JavaScript: Table of Content Using filter and includes MethodsUsing LoopingUsing filter and includes MethodsIn this approach, we a
2 min read
How to Convert Array of Objects into Unique Array of Objects in JavaScript ?
Arrays of objects are a common data structure in JavaScript, often used to store and manipulate collections of related data. However, there are scenarios where you may need to convert an array of objects into a unique array, removing any duplicate objects based on specific criteria. JavaScript has various methods to convert an array of objects into
5 min read
How to Separate Array of Objects into Multiple Objects in JavaScript ?
In JavaScript, the task of separating an array of objects into multiple objects involves organizing and restructuring data based on specific criteria or properties. This process is often necessary when dealing with datasets that need to be grouped or segmented for easier analysis or manipulation. There are various methods to achieve this separation
4 min read
How to Remove Multiple Objects from Nested Array of Objects in JavaScript ?
A nested array of objects is an array of arrays that contain multiple objects as their elements. Removing multiple objects from the nested array of objects in JavaSript can be accomplished in different ways as listed below: Table of Content Using filter() with some() methodUsing filter() with includes() methodUsing filter( ) with findIndex() method
5 min read
How to Remove Null Objects from Nested Array of objects in JavaScript ?
Removing null objects from the nested array of objects can be done by iterating over the array, filtering out the null objects at each level, and then applying a recursive approach to remove all the null objects. This makes sure that all the levels of the nested structure are checked and the null objects are removed. There are various approaches to
4 min read
How to loop through an array containing multiple objects and access their properties in JavaScript ?
Looping through an array of elements is straightforward because you can access them using their indexes. However, it's a bit different with objects since they consist of key-value pairs, and you can't index them like an array. To loop through an array of objects in JavaScript, you need to use different methods compared to traditional arrays. JavaSc
4 min read
How to Update an Array of Objects with Another Array of Objects using Lodash?
Updating an array of objects with another array of objects involves merging or updating objects in the first array with corresponding objects from the second array. Below are the approaches to update an array of objects with another array of objects in the Lodash library: Table of Content Using _.merge function from lodashUsing _.mergeWith function
2 min read
Extract unique objects by attribute from array of objects
Given an array of objects and the task is to return the unique object by the attribute. Examples: Input: [ { name: 'Geeks', id: 10 }, { name: 'GeeksForGeeks', id: 10 }, { name: 'Geeks', id: 20 }, { name: 'Geeks', id: 10 } ]Output:[ { name: 'Geeks', id: 10 }, { name: 'GeeksForGeeks', id: 10 } ]Approach: Let's assume that name is an attribute that di
4 min read
What is the difference between Host objects and Native objects ?
In this article, we will learn about what are Host objects and Native objects, and their differences. JavaScript objects are broadly classified into 2 categories - native javascript objects and host javascript objects. Native objects: Native javascript objects are standard javascript objects which are provided by javascript itself. They are also kn
2 min read
How to access mapped objects in another function in ReactJS ?
Following is the simple approach to access objects of array.map() to another function in React We can directly call the function in array.map() and pass the object in that function to return the required value. Setting up environment and Execution: Step 1: Create React App command npx create-react-app foldernameStep 2: After creating your project f
2 min read