Skip to content
Advertisement

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"
   }
]

Advertisement

Answer

Here is my solution

const output = data.map(i=>Object.values(i).filter(v=>v.name==="sam")).reduce((holder, cur)=>{
    return [...holder,...cur]
},[])
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement