Filter Nested Object In JavaScript

In this article, we are going to learn how to filter nested object in JavaScript.

For this, we will use filter() function to filter the nested objects easily.

Okay!! now let me tell you a little about nested objects.

It is nothing but one object inside another object.

Creating Nested Object

Here is how we can do it step-by-step

  • I have created an array.
  • Also given an array name as nestedObject.
  • In this NestedObject array,  I have stored a nested object.
  • Namely personId and itemDetails.
  • In this itemDetils object, we have three properties name, surname and age respectively.
  • As can see we have to do the creation of nested object.

Nested Object is as follows:

var nestedObject=

[

{personId: 1,

itemDetails: {name: "sanjana", surname:"chawadimani", age:20}},

{personId: 2,

itemDetails: {name: "ana", surname:"patil", age:17}},

{personId: 3,

itemDetails: {name: "shubham", surname:"khamkar", age:25}},

]

Filter() Function in JavaScript

Filter() method allows you to do tasks in a shorter and cleaner way.

Also, filter out things that you want filter in the array and give you an appropriate result.

Syntax:

arrayObject.filter(callback,object);

It accepts the two named arguments: a callback function and an optional object.

CallBack Function:

It’s going to call a function once for each item in the array of objects.

The function is only going to return true or false instead of creating some new array.

If it is true, it returns that condition matched object.

If it returns false that item won’t be passed in it.

NOTE: we won’t modify the actual array.

We called the filter() method of the itemNames array object.

Now we want to filter out in the itemDetails that is who an adult.

For this, we have to give the condition.

Every value of the array should be checked and give us all the values which are more than or equal to 18

So whatever condition will satisfy filter() method returns it.

Let’s see how we can do that,

Our Code: Filter Nested Object

<html>
<scipt>
var nestedObject= [
                  {personId: 1, 
                   itemDetails: {name: "sanjana", surname:"chawadimani", age:20}
                  }, 
                  {personId: 2, 
                  itemDetails: {name: "ana", surname:"patil", age:17}
                  }, 
                  {personId: 3, 
                  itemDetails: {name: "shubham", surname:"khamkar", age:25}
                  } 
                  ]              
                 
                  let itemNames=nestedObject.filter(
                      eachObj=> eachObj.itemDetails.age>=18);
                  document.getElementById("Output").innerHTML
                      =JSON.stringify(itemNames);                       

</scipt>
</html>

 

Output:

[{"personId":1,"itemDetails":{"name":"sanjana","surname":"chawadimani","age":20}},{"personId":2,"itemDetails":{"name":"shubham","surname":"khamkar","age":25}}]

Also Read:

Round a number to 2 decimal places in JavaScript

How to replace all occurrences of a string in JavaScript

Leave a Reply

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