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

159
Visualizações
How to remove another keys of object in array with JavaScript?

I have an array object same like below and I want to get the result only 3 keys id, token and name:

[
 {
  id:1,
  token: 'xyz',
  name: 'john',
  _v: 2,
  _isActived: true,
  _isBlocked: false,
  ...
 },
 {
  id: 2,
  token: 'abc',
  name: 'thomas',
  _v: 2,
  _isActived: true,
  _isBlocked: false,
  ...
 },
 ...
]

My desired result:

[
 {
  id:1,
  token: 'xyz',
  name: 'john'
 },
 {
  id:2,
  token: 'abc',
  name: 'thomas'
 },
 ...
]

Who can help me please!

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

0

Array.map will do the trick.

I have made use of object destructuring as well.

const data = [
 { id:1, token: 'xyz', name: 'john', _v: 2, _isActived: true, _isBlocked: false },
 { id: 2, token: 'abc', name: 'thomas', _v: 2, _isActived: true, _isBlocked: false },
];
const output = data.map(({id, name, token}) => ({id, name, token}));
console.log(output);

If you have a list of allowed keys. You can incoperate this in the map function. Just loop through this array inside the map and add this to an object.

Working Fiddle

const data = [
 { id:1, token: 'xyz', name: 'john', _v: 2, _isActived: true, _isBlocked: false },
 { id: 2, token: 'abc', name: 'thomas', _v: 2, _isActived: true, _isBlocked: false },
];
const list_keys_allowed = ['id', 'token', 'name'];
const output = data.map((node) => {
    const newObj = {};
    list_keys_allowed.forEach(key => newObj[key] = node[key])
    return newObj;
});
console.log(output);

The above statements will create a new array from existing.

If you want to update original array, you could follow the below logic.

Logic

  • Loop through the array.
  • Access the nodes from object.
  • Loop through the remaining keys and delete them from the objects in the array.

Working Fiddle

const data = [
    { id: 1, token: 'xyz', name: 'john', _v: 2, _isActived: true, _isBlocked: false },
    { id: 2, token: 'abc', name: 'thomas', _v: 2, _isActived: true, _isBlocked: false },
];
data.forEach((data) => {
    const { id, name, token, ...restNodes } = data; // ...restNodes will collect all keys except id, name and token
    Object.keys(restNodes).forEach((key) => delete data[key]);
});
console.log(data);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use this

const newArray = myArray.map(object => ({object.id, object.token, object.name}))

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