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

199
Visualizações
How can I filter by object key from array of object in js

I am trying to remove the Id from the array of objects by filtering but the id is not removed. I have used the filter method then I am filtering object keys.

const data= [{
    "id": 1,
    "group_name": "Science",
    "date": 2023,
    "created_table_at": "2022-08-20T01:22:40.000Z",
    "roll": "1401",
    "name": "Israt Jahan",

}]
const filteredData = data.filter((result) =>
          Object.keys(result)
            .filter((key) => key !== "id")
            .reduce((obj, key) => {
              obj[key] = result[key];
              return obj;
            }, {})
        );
console.log(filteredData)

My Expected output:

[{
        "group_name": "Science",
        "date": 2023,
        "created_table_at": "2022-08-20T01:22:40.000Z",
        "roll": "1401",
        "name": "Israt Jahan",

    }]
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You could try this solution:

const data = [{
  id: 1,
  group_name: 'Science',
  date: 2023,
  created_table_at: '2022-08-20T01:22:40.000Z',
  roll: '1401',
  name: 'Israt Jahan'
}];

/**
 * For each object in the data array, keep everything except the id.
 */
const filteredData = data.map(({ id, ...rest }) => rest);

console.log(filteredData);

about 4 years ago · Santiago Trujillo Relatório

0

Filter is for filtering an element in an array, instead you can use map, where we clone the original object, then delete the property we don't want, also this solution only deletes a single property, not multiple ones!

const data = [{
  "id": 1,
  "group_name": "Science",
  "date": 2023,
  "created_table_at": "2022-08-20T01:22:40.000Z",
  "roll": "1401",
  "name": "Israt Jahan",

}]
const filteredData = data.map(x => {
 const cloned = JSON.parse(JSON.stringify(x));
 delete cloned.id;
 return cloned;
});
console.log(filteredData)

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