Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
how to filter nested objects based on properties

Below is a nested array of objects, how can I filter this based on object property? ex. if name==="same" then it should return that object

const arrNestedObj=[
          {
            "id":"1",
            0:{
              "name":"john",
              "age":"10"
            },
            1:{
              "name":"sam",
              "age":"20"
            }
          },{ 
            "id":"2",
            0:{
              "name":"sam",
              "age":"15"
            },
          }
        ]
expected output:
if name==="same" then it should return that object
[
  {
    "name":"sam",
    "age":"20"
  },
  {
     "name":"sam",
     "age":"15"
   }
]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If your meaning is that you want the name === 'sam' object, then can use Array.prototype.map() and Object.getOwnPropertyNames
These two method can help you to get the objects.

example:

<script>
const arrNestedObj=[
          {
            "id":"1",
            0:{
              "name":"john",
              "age":"10"
            },
            1:{
              "name":"sam",
              "age":"20"
            }
          },{ 
            "id":"2",
            0:{
              "name":"sam",
              "age":"15"
            },
          }
        ]

let result = [];
arrNestedObj.map((item) => {
  let tempArray = Object.getOwnPropertyNames(item)
    for(let i=0; i<tempArray.length-1; i++)
    {
     if(Number(tempArray[i] !== NaN))
     {
       if(item[tempArray[i]].name === "sam")
       {
        result.push(item[tempArray[i]]);
       }
     }
    }
});

console.log(result)
</script>

about 4 years ago · Juan Pablo Isaza Report

0

Use the Array.prototype.filter method.

const filteredArr = arrNestedObj.filter(elem => elem.name === "sam")
about 4 years ago · Juan Pablo Isaza Report

0

Here is my solution

const output = data.map(i=>Object.values(i).filter(v=>v.name==="sam")).reduce((holder, cur)=>{
    return [...holder,...cur]
},[])
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!