DEV Community

sah-shah7
sah-shah7

Posted on

Array Of Objects - Part 2

This is in continuation , if you haven’t read Part 1 , please go through it(2 min read) before starting this one.

In the last part we dealt with creation of Array,json, and array of objects. In this part , we’ll cover the popular array methods, how to loop through array of objects.

Starting with using array functions on array of Object!

For this part , we’ll be using the same array of objects, but modifying its contents.
Alt Text

Here some methods such as unshift(), push() and splice() and how they function is shown in the picture above

A short summary :

  • .unshift(variable or object) adds a new object in the beginning
  • .push(variable or object) adds a new object at the end.
  • .splice({index where to start},{how many items to remove},{items to add}) can be used to insert object at any index in the array.

Looping Through an Array of Objects

Knowing how to loop through the array is very important to perform search and filtering.
Let’s start with a challenge, Find all the objects whose rating is above or equal to 50
For that we will need to loop through each object.
Alt Text

All the objects with rating >= 50 were returned. Looping through an array of objects is the same as the index of a normal array. Additionally we’ll have to add the key name for which we want the value.
Another way of searching and filtering can be done by array.find() for searching, array.filter() for filtering where in multiple conditions can be used.

Discussion (0)