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

122
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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