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

123
Visualizações
Change Object Value using forEach

I am trying to change the value of object from the array but, it's not work as expected. I tried following.

const arrObj = [
  {
    "label": "test1",
    "value": 123,
    "type": "number",
    "field": {
      "label": "another",
      "description": "abcd"
    }
  },
  {
    "label": "test2",
    "value": 111,
    "type": "number"
  },
]

arrObj.forEach(obj => {
  obj = {...obj, ...obj.field}
  delete obj.field
})

console.log("after:", arrObj);

Also I found some solution that to use index but, it add index before the object.

const arrObj = [
  {
    "label": "test1",
    "value": 123,
    "type": "number",
    "field": {
      "label": "abcd",
      "description": "abcd"
    }
  },
  {
    "label": "test2",
    "value": 111,
    "type": "number"
  }
]

arrObj.forEach((obj, index) => {
  obj[index] = {...obj, ...obj.field}
  delete obj.field
})

console.log("after:", arrObj);

How can I do with forEach?

Edit:

I want to remove the field object and assign/overwrite all the property outside.

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

0

I would use map to change an array, but you may have a reason that you wish to modify the original. You could just reassign arrObj to the output of the map.

const arrObj = [
  {
    "label": "test1",
    "value": 123,
    "type": "number",
    "field": {
      "label": "another",
      "description": "abcd"
    }
  },
  {
    "label": "test2",
    "value": 111,
    "type": "number"
  },
]

const newArr = arrObj.map(( obj ) => {
 const {field, ...rest} = obj
 return {...field, ...rest}
})

console.log("after:", newArr);

about 4 years ago · Juan Pablo Isaza Relatório

0

Using map and assigning the result is probably a better way of doing this, but if you want to use forEach, you need to assign to the original array inside the loop:

const arrObj = [
  {
    "label": "test1",
    "value": 123,
    "type": "number",
    "field": {
      "label": "another",
      "description": "abcd"
    }
  },
  {
    "label": "test2",
    "value": 111,
    "type": "number"
  },
]

arrObj.forEach(({ field, ...rest}, idx, orig) => {
  orig[idx] = { ...rest, ...field }
})

console.log(arrObj);

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