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 ?
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);