Reputation: 7601
I have a function loops through an array, rearranges its objects, and returns the result (excluding the current object):
// buildings = [{ objectId: '1', objectId: '2', objectId: '3' }]
// currentBuilding = { objectId: '1' }
const result buildings.map(building => {
if (building.objectId === this.currentBuilding.objectId) {
return
}
return {
newField: building.objectId,
}
})
return result
I want it to return:
This function will return:
[{ newField: '2', newField: '3' }]
However, now I'm getting:
[{ undefined, newField: '2', newField: '3' }]
Why is this and how to fix it?
Upvotes: 0
Views: 138
Reputation: 30082
You can't, but you can filter out the offending item:
const result buildings.filter(b => b.objectId !== this.currentBuilding.objectId)
.map(b => ({newField: b.objectId});
Upvotes: 2