You are currently viewing 2 Simple Method to Access Array of Objects in JavaScript

2 Simple Method to Access Array of Objects in JavaScript

How to access array of objects in Javascript

If you don’t know how to access an array of objects in JavaScript then this article is for you. Let me tell you two simple methods to access an array of objects in JavaScript.

Accessing Array of Object

Syntax:

// Dot Notation
document.write(object name[array index number].property name) 

//(or)

//Bracket Notation
document.write(object name[array index number]['property name'])

If you have created an object in an array, to access it you first give the object name and then select the array index inside the square brackets (.) dot and give the property name you want to display. Or you can even use Bracketnotation instead of dot notation as in the second syntax above.

Example:

//array of objects
var data = [
{ user:'Ram', No:123},
{ user:'Raju', No:456}
]

// Dot Notation
document.write(data[0].user)
//Bracket Notation
document.write(data[1]['user'])

Output:how to access array of objects in javascript

Steps:

  1. In the above code, I create a javascript object and name it “data”.
  2. I have created an array in alternative object values and given 2 array values within curly braces. And I have given 2 properties and property values for each array value.
  3. Now an array of objects is created in JavaScript. You can access the array of objects to use the above dot notation or bracket notation syntax.
  4. The javascript print syntax “document.write()” should be used to display the value. To display the property value Raj, first, write the Objectname “data” and then give “0” inside the square bracket because “Ram” is placed at array index 0.
  5. Then use the (.) dot operator to access an object and give the property name “user”, because the user contains “ram”. Similarly, you can use the Bracket Notation method.
READ ALSO  3 Easy Methods to Convert String to Byte Array in JavaScript

Accessing Nested Array of Object

Syntax:

// Dot Notation
document.write(objectname.propertyname[arrayindex].nested propertyname

//Bracket Notation
document.write(objectname.propertyname[arrayindex]['nested propertyname']

If you have a nested data structure containing objects and arrays, to access the value you should follow the above syntax. You can use both dot and bracket notation syntax to access an array of objects in javascript. See the example code and steps given below:

Example:

//Array of objects
var data = {
code: 42,
items: [
{ id: 1, name: 'John' },
{ id: 2, name: 'Alex' }
] }

// Dot Notation
document.write(data.items[0].name)
// Dot Notation
document.write(data.items[1]['name'])

Result:how to access array of objects in javascript

Steps:

  1. In the above code, I create a var type object named “data”. I have created an array in the “item” property value​​ and give 2 array values ​​inside curly braces. And I have given 2 properties and property value for each array value.
  2. Now, I want to display the “john” property value, So as follows the above syntax. First, we need to use documents.write() syntax to display the value.
  3. Inside the print bracket, I give the object name “data” and select the “item” property using the dot operator because the john value is placed inside the item property value.
  4. Then give array index 0 within the square brackets because the John value is placed in index value 0. And finally, give the property “name”.
    5. Similar to this method, you can also use bracket notation in Javascript.

Conclusion

I have told 2 methods, how to access an array of objects in javascript. And if you have any doubts, please let us know in the comments.

Leave a Reply