Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
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?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório

0

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);
       })
    })
about 4 years ago · Juan Pablo Isaza Relatório

0

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)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda