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

139
Visualizações
How to remove property from object

We have an array of objects like this:

const arrOfObjects = [
  {
    name: 'Ronaldo',
    age: 20,
    status: true
  },
  {
    name: 'Messi',
    age: 30,
    status: false
  },
  {
    name: 'Benzema',
    age: 40,
    status: false
  },  
  {
    name: 'Vini',
    age: 50,
    status: false
  }
]

I would like to create new array of objects, based on this array, and add some new properties - so the map will be the best way.

const newArray = arrayOfObjects.map(obj => {
    return {
    ...obj,
    newProperty: 'newProperty',
    age:  // Here I want to check if status property is true, if yes, I would like to remove property age from current object
  }
})

I would like to remove age property from object, if status: true. How can I do it?

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

0

Extract the age with destructuring and use a conditional part to insert it again:

const arrOfObjects = [{name: 'Ronaldo',age: 20,status: true},{name: 'Messi',age: 30,status: false},{name: 'Benzema',age: 40,status: false},{name: 'Vini',age: 50,status: false}];

const newArray = arrOfObjects.map(obj => {
    let {age, ...rest} = obj;
    return {
        ...rest,
        newProperty: 'newProperty',
        ...!obj.status && {age} 
    }
});

console.log(newArray);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the delete operator to remove a property

const arrOfObjects = [
  {
    name: 'Ronaldo',
    age: 20,
    status: true
  },
  {
    name: 'Messi',
    age: 30,
    status: false
  },
  {
    name: 'Benzema',
    age: 40,
    status: false
  },  
  {
    name: 'Vini',
    age: 50,
    status: false
  }
]

const newArray = arrOfObjects.map(obj => {
  if(obj.status) delete obj.age
    return {
    ...obj,
    newProperty: 'newProperty',
  }
})

console.log(newArray)

Note : As @trincot mentionned, this will remove the age value from the original object.

about 4 years ago · Juan Pablo Isaza Relatório

0

The long way but with good performance.

 const arrOfObjects = [
  {
    name: 'Ronaldo',
    age: 20,
    status: true
  },
  {
    name: 'Messi',
    age: 30,
    status: false
  },
  {
    name: 'Benzema',
    age: 40,
    status: false
  },  
  {
    name: 'Vini',
    age: 50,
    status: false
  }
]

const n = []
arrOfObjects.forEach(a => {
  const tmp = {}
  tmp.newProperty = 'newProperty';
  tmp.name = a.name;
  tmp.status = a.status
  if (! a.status) {
    tmp.age = a.age
  }
  n.push(tmp);
})

console.log(n)

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