Home > Back-end >  Filtering on nested array of objects to return the entire matching object
Filtering on nested array of objects to return the entire matching object

Time:07-19

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.

CodePudding user response:

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)

  • Related