Home > Software engineering >  Filtering array of nested objects in JavaScript up to 'n' levels
Filtering array of nested objects in JavaScript up to 'n' levels

Time:09-26

I want to filter the array of nested objects in javascript.

I got the below answer from other question.

var sampleData= [{       
        "rowId": "3.0.0",
        "startDate": "2020-10-20",
        "subTasks": [                 
            {                
                "rowId": "3.3.0",
                "startDate": "2021-05-26",             
                "subTasks": [
                    {
                        "rowId": "3.3.0.1",
                        "startDate": "2021-05-26",                        
                        "subTasks": []
                    },
                    {
                        "rowId": "3.3.0.2",
                        "startDate": "2021-06-09",
                        "endDate": "2021-07-23",  
                        "subTasks": []                      
                    },                   
                ]
            },           
        ]
    }]

    filtered = sampleData.map(element => {
        return {
          ...element,
          subTasks: element.subTasks.filter(subElement => {
            return subElement.endDate
          })
        }
      })
      console.log("sampleData",JSON.stringify(filtered))




I want to filer based on the end date. Expected output: Object with rowId "3.3.0.2" need to be filtered.

This code is filtering only up to 2 levels. But in my case the nested objects can grow up to 10 levels. How can I filter the array of objects up to n levels?

CodePudding user response:

You can do it with a recursive function and loop over only the valid subtasks. I do not think it needs much of an explanation, assuming you know what a recursive function is (and I guess you do):

function recurseOnSubTasks(elem) {
  const filteredSubTasks = elem.subTasks
    .filter(subTask => !subTask.endDate)
    .map(subTask => recurseOnSubTasks(subTask))
  
  return {
    ...elem,
    subTasks: filteredSubTasks
  }
}

And then call it with:

sampleData.map(elem => recurseOnSubTasks(elem))

var sampleData= [{       
        "rowId": "3.0.0",
        "startDate": "2020-10-20",
        "subTasks": [                 
            {                
                "rowId": "3.3.0",
                "startDate": "2021-05-26",             
                "subTasks": [
                    {
                        "rowId": "3.3.0.1",
                        "startDate": "2021-05-26",                        
                        "subTasks": []
                    },
                    {
                        "rowId": "3.3.0.2",
                        "startDate": "2021-06-09",
                        "endDate": "2021-07-23",  
                        "subTasks": []                      
                    },                   
                ]
            },           
        ]
    }];

function recurseOnSubTasks(elem) {
    const filteredSubTasks = elem.subTasks
    .filter(subTask => !subTask.endDate)
    .map(subTask => recurseOnSubTasks(subTask))
  
  return {
    ...elem,
    subTasks: filteredSubTasks
  }
}

console.log(sampleData.map(elem => recurseOnSubTasks(elem)));

CodePudding user response:

you can do that by using recursion. values array will contain all tasks then you can do filtration on any property you want.

var values = [];
function GetValues(samples){

    if(samples != [] && samples != undefined && samples != null){
        for(var sample of samples){       
            values.push(sample)
            GetValues(sample.subTasks)
                                  }

    }else{
        return {};
    }

}
GetValues(sampleData)
  • Related