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

125
Vistas
Remove data from my nested array of objects by matching values

Remove data from my nested array of objects by matching values. In my case I want to strip out the objects that are NOT active. So every object that contains active 0 needs to be removed.

[
    {
        "id" : 1,
        "title" : 'list of...',
        "goals": [
            {
                "id": 1569,
                "active": 0
            },
            {
                "id": 1570,
                "active": 1
            },
            {
                "id": 1571,
                "active": 0
            }
        ],
    },
    {
        "id" : 2,
        "title" : 'more goals',
        "goals": [
            {
                "id": 1069,
                "active": 0
            },
            {
                "id": 1070,
                "active": 1
            },
        ],
    },
]

The following will return the array in an unchanged status

public stripGoalsByInactiveGoals(clusters) {
    return clusters.filter(cluster =>
        cluster.goals.filter(goal => goal.active === 1)
    );
}
about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

array.filter wait a boolean to know if it has to filter data or not

in your case you have an array of array, you want to filter "sub" array by active goal

if you want to keep only active goals change your first filter by map to return a modify value of your array filtered by a condition

function stripGoalsByInactiveGoals(clusters) {
  return clusters.map(cluster => {
    return {
      goals: cluster.goals.filter(goal =>  goal.active)
    };
  });
}

var data = [{
    "goals": [{
        "id": 1569,
        "active": 0
      },
      {
        "id": 1570,
        "active": 1
      },
      {
        "id": 1571,
        "active": 0
      }
    ],
  },
  {
    "goals": [{
        "id": 1069,
        "active": 0
      },
      {
        "id": 1070,
        "active": 1
      },
    ],
  },
];

function stripGoalsByInactiveGoals(clusters) {
  return clusters.map(cluster => {
    return {
      goals: cluster.goals.filter(goal => goal.active)
    };
  });
}

console.log(stripGoalsByInactiveGoals(data));

about 4 years ago · Santiago Gelvez Denunciar

0

You can create another array (for the case when you need the input unchanged as well) and loop the input, appending each member objects' filtered goals array. You could also avoid appending the item if goals is empty after the filter, but this example doesn't do this, because it was not specified as a requirement.

let input = [
    {
        "goals": [
            {
                "id": 1569,
                "active": 0
            },
            {
                "id": 1570,
                "active": 1
            },
            {
                "id": 1571,
                "active": 0
            }
        ],
    },
    {
        "goals": [
            {
                "id": 1069,
                "active": 0
            },
            {
                "id": 1070,
                "active": 1
            },
        ],
    },
]

let output = [];
for (let item of input) {
    output.push({goals: item.goals.filter(element => (element.active))})
}

console.log(output);

about 4 years ago · Santiago Gelvez Denunciar

0

You can follow this for a dynamic approach:

 stripGoalsByInactiveGoals(clusters) {
    var res = [];

    this.data.forEach((item) => {
        let itemObj = {};
        Object.keys(item).forEach((key) => {
            itemObj[key] = item[key].filter(x => x.active != 0);
            res.push(itemObj);
        });
    });

    return res;
}

Stackbiltz Demo

about 4 years ago · Santiago Gelvez 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