1

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.

2
  • organisation.filter(o => o.dept.employees.some(e => e.address.some(a => a.state === 'NY'))) 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 Commented Aug 4, 2021 at 7:53

5 Answers 5

3

You can use Array.prototype.filter, Array.prototype.some, and Array.prototype.find functions for achieving your required outcome. 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".

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Sajeeb i will test this and update here
2

You can use Array.prototype.filter with combination 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'.

Comments

1

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);

Comments

0

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);

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.