How to Count JavaScript Array Objects

How to Count JavaScript Array Objects? We will learn how to count JavaScript array objects in this tutorial. In JavaScript, the data structure that houses the texts, numbers, objects, etc. is an array. The only type of entity that has associated properties and methods is an object. Using the object’s references, we may call the object method and gain access to the object’s properties.

Typically, we may use the array.length() method to get the length of an array, which returns the array’s total number of elements. What if, however, we only need to count object elements?

This tutorial offers several methods for counting the object elements in the JavaScript array:-

  • Using the instanceof operator and the for loop.
  • Making use of the array.filter() Method.

Count JavaScript Array Objects Using instanceof operator & for loop

This method will use the for loop to count all the objects in the array. Using the instanceof operator, users can traverse through each array element and determine each element’s type. To hold a total number of items, they can initialize the length variable with 0. If they encounter any object entities while iterating through the array items, they can raise the length by one.

Users can count array objects using the for loop and instanceof operator in the following syntax.

let objectsLen = 0;
for (let i = 0; i < myArr.length; i++) {
  // if an entity is an object
  // increase objectsLen by 1
  // which stores the total number of objects in array.
  if (myArr[i] instanceof Object) {
    objectsLen++;
  }
}

Algorithm

Step 1: Create a variable called objectsLen and set its initial value to 0.
Step 2: Construct an array and include particular objects with additional components.
Step 3: Using the for loop to traverse through the array and the instanceof operator to check the element type, we can count the number of objects in the array.
Step 4: If an entity of the object type is found, we will increase the objectsLen variable by 1 in step 4.
Step 5:  Finally, the objectsLen variable, which contains the total number of objects, will be printed.

const storage = [
  { data: "1", status: "0" },
  { data: "2", status: "0" },
  { data: "3", status: "0" },
  { data: "4", status: "0" },
  { data: "5", status: "0" },
  { data: "6", status: "0" },
  { data: "7", status: "1" },
];

let counter = 0;
for (let i = 0; i < storage.length; i++) {
  if (storage[i].status === "0") counter++;
}

console.log(counter);

Output:-

6

Count JavaScript Array Objects Using array.filter()

The array.filter() method in JavaScript filters the array’s elements. Users can add the callback function, and the callback function can also add some filtering conditions. In our situation, checking for the element type is the filtering condition that will filter all objects. We can use the typeof operator to determine an object’s type, and the callback function will return true if the entity is a type of object. If not, return false.

The array returns the array of all filtered values.filter() function. we will then obtain the array of all objects, and its length may be determined using the.length() method. Use filter() as a method to count the number of occurrences in an array.

const storage = [
  { data: "1", status: "0" },
  { data: "2", status: "0" },
  { data: "3", status: "0" },
  { data: "4", status: "0" },
  { data: "5", status: "0" },
  { data: "6", status: "0" },
  { data: "7", status: "1" },
];

const count = storage.filter((item) => item.status === "0").length;

console.log(count);

Output:-

6

Users can calculate the number of object entities in a JavaScript array. The first and second approaches take the same amount of time. When comparing the space complexity of the two ways, we find that the first strategy is more optimal and has a lower space complexity.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *