VBC
VBC

Reputation: 301

Filtering array of nested objects in JavaScript up to 'n' levels

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?

Upvotes: 0

Views: 1959

Answers (3)

Yashawant Sawant
Yashawant Sawant

Reputation: 43

If you want to filter based on endDate, then can use forEach method and put it in an object and then reach out to desired output. Hope this would work.

   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": []                      
                    },                   
                ]
            },           
        ]
}]

  sampleData.forEach((obj)=> {
       obj.subTasks.forEach((prop) => {
           const newObj = {name: prop};
           console.log(newObj.name.subTasks[1]);
           console.log(newObj.name.subTasks[1].endDate);
       })
    })

Upvotes: 0

Nour Khashan
Nour Khashan

Reputation: 5

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)

Upvotes: 0

Marco Luzzara
Marco Luzzara

Reputation: 6026

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)));

Upvotes: 1

Related Questions