2
I have an array of objects containing 5 objects, both with properties x and y:
const objArr = [
{ 'x': 1, 'y': 1 }, // OBJ0
{ 'x': 2, 'y': 3 }, // OBJ1
{ 'x': 3, 'y': 3 }, // OBJ2
{ 'x': 3, 'y': 4 }, // OBJ3
{ 'x': 4, 'y': 5 } // OBJ4
]
I know I can take the property x and y as follows:
const [ { x, y } ] = objArr;
However, I can only store the first object:
console.log(x); // 1
console.log(y); // 1
This is the function that I created, it works that way, but I wanted to know how to rewrite it using unstructuring. I thank you already.
function getCount([...objArr]) {
let samePropCount = 0;
for(let i=0; i<objArr.length; i++) {
if (objArr[i].x === objArr[i].y) {
samePropCount++;
}
}
return samePropCount;
}
Pera a little bit, I don’t understand very well, its function works, but Voce wants to use destructurion instead of
objArr[i].x
?– Cmte Cardeal
It’s more about learning about disruption
– Matheus Henrique
I don’t know if I understand, but I posted an answer. Analyze it
– Cmte Cardeal