admin管理员组

文章数量:1399953

I have a nested array of objects, I'm trying to filter the given array of objects using a property from the third level of its array property value. For example, from the below array I like to filter the entire array using the property "state" = "NY".

organisation = [
   {
      "dept_id":1,
      "dept":{
         "name":"finance",
         "employees":[
            {
               "emp_id":1,
               "name":"John",
               "address":[
                  {
                     "country":"US",
                     "state":"NC"
                  }
               ]
            }
         ]
      }
   },
   {
      "dept_id":2,
      "dept":{
         "name":"marketing",
         "employees":[
            {
               "emp_id":2,
               "name":"David",
               "address":[
                  {
                     "country":"US",
                     "state":"NY"
                  }
               ]
            }
         ]
      }
   },
   {
      "dept_id":3,
      "dept":{
         "name":"sales",
         "employees":[
            {
               "emp_id":3,
               "name":"Robert",
               "address":[
                  {
                     "country":"US",
                     "state":"NC"
                  }
               ]
            }
         ]
      }
   }
]

As a result of using the filter "state":"NY" my expected answer is

organisation = [
   {
      "dept_id":2,
      "dept":{
         "name":"marketing",
         "employees":[
            {
               "emp_id":2,
               "name":"David",
               "address":[
                  {
                     "country":"US",
                     "state":"NY"
                  }
               ]
            }
         ]
      }
   }
]

How can I use filter method using javascript to get this expected result.

I have a nested array of objects, I'm trying to filter the given array of objects using a property from the third level of its array property value. For example, from the below array I like to filter the entire array using the property "state" = "NY".

organisation = [
   {
      "dept_id":1,
      "dept":{
         "name":"finance",
         "employees":[
            {
               "emp_id":1,
               "name":"John",
               "address":[
                  {
                     "country":"US",
                     "state":"NC"
                  }
               ]
            }
         ]
      }
   },
   {
      "dept_id":2,
      "dept":{
         "name":"marketing",
         "employees":[
            {
               "emp_id":2,
               "name":"David",
               "address":[
                  {
                     "country":"US",
                     "state":"NY"
                  }
               ]
            }
         ]
      }
   },
   {
      "dept_id":3,
      "dept":{
         "name":"sales",
         "employees":[
            {
               "emp_id":3,
               "name":"Robert",
               "address":[
                  {
                     "country":"US",
                     "state":"NC"
                  }
               ]
            }
         ]
      }
   }
]

As a result of using the filter "state":"NY" my expected answer is

organisation = [
   {
      "dept_id":2,
      "dept":{
         "name":"marketing",
         "employees":[
            {
               "emp_id":2,
               "name":"David",
               "address":[
                  {
                     "country":"US",
                     "state":"NY"
                  }
               ]
            }
         ]
      }
   }
]

How can I use filter method using javascript to get this expected result.

Share Improve this question edited Aug 4, 2021 at 13:19 AbsoluteBeginner 2,2533 gold badges14 silver badges24 bronze badges asked Aug 4, 2021 at 7:40 Lalas MLalas M 1,1741 gold badge16 silver badges32 bronze badges 2
  • organisation.filter(o => o.dept.employees.some(e => e.address.some(a => a.state === 'NY'))) – abhishekkannojia Commented Aug 4, 2021 at 7:45
  • Thanks abhishekkannojia it is working .. if you can add this as an answer that will be great for reference .. once again thanks – Lalas M Commented Aug 4, 2021 at 7:53
Add a ment  | 

5 Answers 5

Reset to default 3

You can use Array.prototype.filter, Array.prototype.some, and Array.prototype.find functions for achieving your required oute. Try this-

const organisation=[{dept_id:1,dept:{name:"finance",employees:[{emp_id:1,name:"John",address:[{country:"US",state:"NC"}]}]}},{dept_id:2,dept:{name:"marketing",employees:[{emp_id:2,name:"David",address:[{country:"US",state:"NY"}]}]}},{dept_id:3,dept:{name:"sales",employees:[{emp_id:3,name:"Robert",address:[{country:"US",state:"NC"}]}]}}];

const filter = "NY";
const res = organisation.filter(
  item => item.dept.employees.some(
    employee => employee.address.find(address => address.state === filter)
  )
);

console.log(res);

Here, I've applied the filter on the organization array, then check if any employee inside the employees array has the address with the state value "NY".

You can use Array.prototype.filter with bination of Array.prototype.some to filter out the results you want.

const organisation=[{dept_id:1,dept:{name:"finance",employees:[{emp_id:1,name:"John",address:[{country:"US",state:"NC"}]}]}},{dept_id:2,dept:{name:"marketing",employees:[{emp_id:2,name:"David",address:[{country:"US",state:"NY"}]}]}},{dept_id:3,dept:{name:"sales",employees:[{emp_id:3,name:"Robert",address:[{country:"US",state:"NC"}]}]}}];
    
    const result = organisation.filter(
      item => item.dept.employees.some(
        employee => employee.address.some(address => address.state === 'NY')  // Filter condition
      )
    );
    
    console.log(result);

Basically this filters the organization array where some empoloyee has some address with state value 'NY'.

const organisation=[{dept_id:1,dept:{name:"finance",employees:[{emp_id:1,name:"John",address:[{country:"US",state:"NC"}]}]}},{dept_id:2,dept:{name:"marketing",employees:[{emp_id:2,name:"David",address:[{country:"US",state:"NY"}]}]}},{dept_id:3,dept:{name:"sales",employees:[{emp_id:3,name:"Robert",address:[{country:"US",state:"NC"}]}]}}];

const filter = "NY";
const res = organisation.filter(item => item.dept.employees.find(add => JSON.stringify(add.address).includes('"state":"' + filter + '"')));

console.log(res);

you can use filter only if you want.

filtered = organisation.filter(el => el.dept.employees.filter(el => el.address.filter(el => el.state == 'NY').length > 0 ).length > 0);

organisation = [
   {
      "dept_id":1,
      "dept":{
         "name":"finance",
         "employees":[
            {
               "emp_id":1,
               "name":"John",
               "address":[
                  {
                     "country":"US",
                     "state":"NC"
                  }
               ]
            }
         ]
      }
   },
   {
      "dept_id":2,
      "dept":{
         "name":"marketing",
         "employees":[
            {
               "emp_id":2,
               "name":"David",
               "address":[
                  {
                     "country":"US",
                     "state":"NY"
                  }
               ]
            }
         ]
      }
   },
   {
      "dept_id":3,
      "dept":{
         "name":"sales",
         "employees":[
            {
               "emp_id":3,
               "name":"Robert",
               "address":[
                  {
                     "country":"US",
                     "state":"NC"
                  }
               ]
            }
         ]
      }
   }
]


filtered = organisation.filter(el => el.dept.employees.filter(el => el.address.filter(el => el.state == 'NY').length > 0 ).length > 0);

console.log(filtered);

I filtered it like this. Try this

    const filteredOrg = [];
    
    organisation.forEach((item) => {
       item.dept.employees.forEach((employe) => {
        employe.address.forEach((address) => {
          if (address.state === "NY") {
            filteredOrg.push(item);
          }
        });
      });

});

I hope it will work for you too.

Happy coding!

本文标签: javascriptHow to do filter on nested array of object in more than two levelStack Overflow