Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

81
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!