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

96
Visualizações
Array of object and forEach method element access

I have an array of objects defined as such:

const people = [
   {
     name: "John",
     dateOfBirth: 1990,
     dateOfDeath: 2020
   },
   {
     name: "Jane",
     dateOfBirth: 1970,
     dateOfDeath: 2019,
   },
   {
    name: "Micheal",
    dateOfBirth: 2002,
   }
 ]

I want to loop through the whole array and set the dateOfDeath for every person still alive to today.

I used

people.forEach ( element => {
   if(!("dateOfDeath" in element)) 
        element.dateOfDeath = (new date()).getFullYear();
});

My question is, why does this work? how can the callback arrow function access the array element and modify it? shouldn't it modify only the local element variable (which is the function parameter), hence leaving the array item intact?

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

0

Because they both have the same reference in the memory, as the object is a reference type.

For more resources about reference type and primitive type.

https://www.freecodecamp.org/news/primitive-vs-reference-data-types-in-javascript/#:~:text=Reference%20data%20types%2C%20unlike%20primitive,all%20other%20types%20of%20objects.

about 4 years ago · Juan Pablo Isaza Relatório

0

My question is, why does this work? how can the callback arrow function access the array element and modify it? shouldn't it modify only the local element variable (which is the function parameter), hence leaving the array item intact?

It works because the element which you receive, you can think of it as a reference to the original item (it works also because object has reference type, you wouldn't be able to do same with primitive types). So when you do element.something you are actually accessing the original item, via the reference.

about 4 years ago · Juan Pablo Isaza Relatório

0

Actually the "element" passes in the callback has the same reference as the object. In layman terms both variables pointing same location in heap when you use element.dateOfDeath (It demonstrates that go the location which is pointing by element and there change the dateOFDeath). In normal case when you have normal array of numbers or strings this will not work out because in that case element act as a copy of original item present in array.

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