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

102
Visualizações
How can I use the same array of objects for 2 functions in javascript

I have an array that looks like this...

const arr = [
    { name: 'Will', text: "Hey, I'm Will" },
    { name: 'Gary', text: "Time to run" },
    { name: 'Bob', text: "Yo it's Bob" }
]

Now I want to run two different functions that both take the same 'arr' variable as an argument. When I pass it into the first function, this function makes changes to each object, adding an additional property to each object. These changes are being carried over to the next function. It looks something like this...

const func1 = data => {
    data.forEach(item => item.year = '2022')

    console.log(data)   
}

const func2 = data => console.log(data)

[
    { name: 'Will', text: "Hey, I'm Will" },
    { name: 'Gary', text: "Time to run" },
    { name: 'Bob', text: "Yo it's Bob" }
]

//  both functions return ...
    // [
    //  { name: 'Will', text: "Hey, I'm Will", year: 2022 },
    //  { name: 'Gary', text: "Time to run", year: 2022 },
    //  { name: 'Bob', text: "Yo it's Bob", year: 2022 }
    // ]

Is there a way that I can prevent the data that is passed into func2 not being changed by the operations that are performed on the data in func1?

about 4 years ago · Juan Pablo Isaza
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