What is an Array of Objects in JavaScript?

Video Tutorial
FREE
some more fun with Objects thumbnail
This video belongs to
JavaScript Course With Certification: Unlocking the Power of JavaScript
9 modules
Certificate
Topics Covered

An array in javascript is the collection of multiple types of data at a single place in order, and an array of objects in javascript is a collection of homogenous data, that stores a sequence of numbered objects at a single place.

Example-

Output-

Above, there is an array of objects myArr and we are accessing an object from the array by using array index myArr[1], also printing the value on the console. As result, we are printing an object from an array of objects. This prints all the key-value pairs present at that particular index.

Syntax

Array of objects syntax-

Creating an Array of Objects

To create an array of objects in javascript, we have to declare an empty array first and then initialize it with the objects separated by a comma.

Example-

Above, we declared an array of objects where each array is separated by a comma and has three properties in each.

Array Properties

There are three types of properties in array Object: 1.Constructor- The constructor properties return a function that creates the Array prototype. This return function is a native code that is provided by the javascript engine.

Example-

Output-

Above, we have an array of objects myArr and we are printing its constructor properties on the console. As result, we print the function Array() { [native code] } on the console.

Here, we get the native code as a reference which is a predefined function inside the javascript engine responsible for creating the Array prototype.

2. Length- It returns the length of elements in an array i.e. the number of objects present in the array.

Example-

Output-

Above, we use the length properties on the myArr array of objects and also print it on the console. As result, we print 2 on the console.

3. Prototype- Array Prototype property helps to add new methods into the Array, for example- adding all values into an array. It also helps to add properties to the Array Object.

Example-

Output-

Above, we are adding a user-defined method into the Array Object. To do that, we accessed the Array prototype property using dot notation and added the user-defined function add into Array Object like this Array.prototype.add. Then, assigned an anonymous function to add, which is the definition of add method.

In the add method definition, we declare a result variable to store the array value count. Here, we are using the this keyword to reference the array on which the add method will get called and looping over the array. In each iteration, we are accessing the individual array values and adding them to the result variable, when all values inside the array get added to the result variable, we returned the final result.

Looking to build a solid foundation in front-end development? Our JavaScript course is your gateway to success. Sign up today!

Array Methods

Array methods are predefined methods into the Array Object prototype property. To use the array methods we call it on an array using object dot notation, using bracket notation will cause an error.

These are some frequently used array methods:

Array.push()

The push method helps to add a new value at the end of an array. To use it, we have to call the push method on an array by passing the value we want to add as a parameter. We can pass a single value as a parameter or multiple values separated by a comma.

Example-

Output-

Above, calling push method upon myArr array by passing a value 4 as a parameter also printing myArr on the console. As result, value 4 gets added at the end of myArr array, and [1,2,3,4] get printed on the console. The time complexity of the push method is O(1) and space complexity is O(N), where N is the length of the input.

Array.pop()

Array pop method helps to remove the last value from an array, it gives the removed value in return. To use it, we have to call pop() method on an array, which doesn't take any parameter.

Example-

Output-

Above, we have myArr array on which we are calling pop method. As result, the last value of the array myArr gets removed. Time complexity of the push method is O(1) and space complexity is O(1).

Array.shift()

Array shift method helps to remove the first value from an array, and in return, it gives the removed value. To use shift method, we have to call it on an array, and it doesn't take any parameter.

Example-

Output-

Above, we have myArr array on which we are calling shift method. As result, the first value of the array myArr gets removed. Time complexity of push method is O(n) and space complexity is O(1). Use this Free Online JavaScript Compiler to compile your code.

Array.unshift()

Unshift method unshift to add a value at the beginning of an array. To use it, we have to call it on an array by passing the value we want to add as a parameter. We can pass a single value as a parameter or multiple values separated by a comma.

Example-

Output-

Above, calling unshift method on myArr array by passing a value 4 as a parameter. As result, value 4 gets added at the beginning of myArr array. Time complexity of push method is O(n) and space complexity is O(N), where N is the length of the input.

Array.slice()

Array slice method helps to copy an array without affecting the original array. To use it, we have called it on an array by passing the first and last index of the subarray we want to copy, but the value of the last index will not be included in the copied subarray. Leaving parameters blank will result in slice method default behavior, which will copy the whole array from index [0] to [n].

Example-

Output-

Above, we have myArr array on which we are calling slice method by passing first index 1 and last3 as parameters. As result, we copy the subarray from index [1] to [3] which is [2,3]. Time complexity of slice method is O(n) and space complexity is O(N), where N is the length of the input. Time complexity of push method is O(n logn) and space complexity is O(N), where N is the length of the input.

Looping through an Array of Objects

We can loop through an array of objects using the for loop but available array methods make it much easier. So, we are going to use the map method to loop through an array of objects.

Output-

Above, we loop over an array of objects myArr using map method, in each iteration again looping over the individual objects and printing the properties key-value pair on the console. As result, we printed all the array object's properties on the console.

Conclusion:

  • Array of objects in javascript is just an array having object data as its value.
  • Array methods are predefined methods that help to perform various operations on an array.
  • To make an array of objects in javascript, we have to declare an array and then add the object's data as its value.