I'm trying to go through an array of objects to extract two properties to another object. I have the following function to achieve this. While it works for the nested property correctly, I cant extract non-nested property and keep getting undefined.
const formatPhonesArray = (array) => {
console.log(array);
let resultArray = [];
for (const phone of array) {
resultArray.push({
operator: phone.operator.name,
phone: phone.number,
});
}
console.log(resultArray);
return resultArray;
};
And I have the following array
[
{
_id: new ObjectId("6190ecde73b2623593666fbb"),
number: '+38(067)341-32-92',
operator: {
_id: new ObjectId("616090760a30fb3f993b61b5"),
name: 'Київстар',
codes: [Array],
__v: 0
}
}
]
But somehow it yelds this
[ { operator: 'Київстар', phone: undefined } ]
while I expect
[ { operator: 'Київстар', phone: '+38(067)341-32-92'} ]
and I can't figure out why.
No problem in your code I got this result
[
{
"operator": "Київстар",
"phone": "+38(067)341-32-92"
}
]
Its working