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

84
Visualizações
accessing objects in new array

const lineExampleOne = 
  [ { weight: 150, floor: 2 } 
  , { weight: 200, floor: 3 } 
  , { weight: 120, floor: 5 } 
  , { weight:  80, floor: 2 } 
  , { weight: 180, floor: 4 } 
  , { weight: 170, floor: 4 } 
  ];

let newArray = [];

lineExampleOne.forEach((person)=>{
  newArray.push(person);
})

console.log(newArray);

The console returns something like this [object Object],[object Object] etc...

How do I correctly add each object to my empty newArray?
How do I properly access each objects properties from my newArray?
and is there a way to test how many unique values I have for floors and weight of all the objects in my newArray ?

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

0

The console returns something like this [object Object],[object Object] etc...

That's just how the console logs objects. If you want more insight when logging collections like this I suggest using console.table(newArray).

How do I correctly add each object to my empty newArray?

Your code looks correct. Could be more succinct using map if interested (since map returns a new array). E.g. const newArray = lineExampleOne.map((a) => a);

How do I properly access each objects properties from my newArray?

You'll need to use find. E.g. const item = newArray.find(({ weight }) => weight === 200); console.log(item.weight). find will return the first match. If you want all matches then you'll want to use filter.

is there a way to test how many unique values I have for floors and weight of all the objects in my newArray.

Yes. Set will dedupe arrays, so you can use it for properties on a collection like so.

const distinctWeights = [...new Set(newArray.map((a) => a.weight))];
console.log(distinctWeights.length);
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