Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda