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

177
Views
Filtering on nested array of objects to return the entire matching object

My array of nested objects looks like this along with the array to match it with

let findThis = ["Water"];
    let arrayOfElements = 
    [
            {
               "code": "a",
               "templates": [
                 {
                   "templateCode": "Water",
                   "title": "Earth"
                 },
                 {
                   "templateCode": "Milk",
                   "title": "Sky"
                 }
                 
                ]
            },
            {
               "code": "b",
               "templates": []
            },
            {
               "code": "c",
               "templates": [
                 {
                   "templateCode": "Water",
                   "title": "Earth"
                 },
                 {
                   "templateCode": "Tree",
                   "title": "Moon"
                 }
            },
            {
               "code": "d",
               "templates": [
                 {
                   "templateCode": "Tooth",
                   "title": "Tiger"
                 }
            }
    ]

I want to extract those objects whose templateCode is Water. So my final returned array should look like this.

let result = 
[
        {
           "code": "a",
           "templates": [
             {
               "templateCode": "Water",
               "title": "Earth"
             },
             {
               "templateCode": "Milk",
               "title": "Sky"
             }
             
            ]
        },
        {
           "code": "c",
           "templates": [
             {
               "templateCode": "Water",
               "title": "Earth"
             },
             {
               "templateCode": "Tree",
               "title": "Moon"
             }
        }
]

I tried:

arrayOfElements.filter(x => x.templates.filter(y => findThis.includes(y.templateCode)));

But it is returning the entire array itself and is not working.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You need to filter the arrayOfElements with the condition of any templateCode existing inside the findThis array.

let findThis = ["Water"];
    let arrayOfElements = 
    [
            {
               "code": "a",
               "templates": [
                 {
                   "templateCode": "Water",
                   "title": "Earth"
                 },
                 {
                   "templateCode": "Milk",
                   "title": "Sky"
                 }
                 
                ]
            },
            {
               "code": "b",
               "templates": []
            },
            {
               "code": "c",
               "templates": [
                 {
                   "templateCode": "Water",
                   "title": "Earth"
                 },
                 {
                   "templateCode": "Tree",
                   "title": "Moon"
                 }
                 ]
            },
            {
               "code": "d",
               "templates": [
                 {
                   "templateCode": "Tooth",
                   "title": "Tiger"
                 }
                 ]
            }
    ]
    
const result = arrayOfElements.filter(item => item.templates.some(t => findThis.includes(t.templateCode)))

console.log(result)

about 4 years ago · Santiago Gelvez 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!