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

143
Visualizações
Replace nested array for new array

I have an array inside another array:

Structure:

First Array
[
{
Second Array
   [
     {
     }
   ]
}
]

So I want to replace all Second Array for new array that I have so I try this:

this.FirstArray.map(
          (myArray) =>
            myArray.SecondArray === this.MyNewArray
        )

But this is not replacing for my new array it still having old values. What am I doing wrong?

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

0

Normally, the map() method will return a new array.

// Assume that your nested array looks like so:
let nestedArray = [
    ["A", "B", "C"],
    ["E", "F", "G"]
]

// Assume that this is the new array
const newArray = [1, 2, 3]

nestedArray = nestedArray.map(el => newArray);

// expected output: nestedArray [[1, 2, 3], [1, 2, 3]]
about 4 years ago · Juan Pablo Isaza Relatório

0

You can achieve that by using spread operator and without any loop as you want to replace second array with the new array.

Input array :

First Array
[{
  Second Array
   [{}]
}]

Logic to replace newArray with secondArray :

FirstArray[0].secondArray = [ ...newArray ]

Demo :

const firstArray = [{
    secondArray: ['alpha', 'beta', 'gamma']
}];

const newArray = ['A', 'B'];

firstArray[0].secondArray = [ ...newArray ];

console.log(firstArray);

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