I have this array
const buildings = [
{ id: 111, status: false, image: 'Test1' },
{ id: 334, status: true, image: 'Test4' },
{ id: 243, status: false, image: 'Test7' },
{ id: 654, status: false, image: 'Test9' },
{ id: 222, status: true, image: 'Test8' }
];
What I need is to update same value based on new porperti
cons newBuilding = { id: 111, status: true, image: 'Test1' };
I need a function that will handle like that like this
mergeSelectedBuildings(building) {
if (buildings.length >= 0) {
buildings.push(building);
} else {
buildings.map((buildingValue, i) => {
if (buildingValue.id === building.id) {
this.buildings[i].status = building.status;
this.buildings[i].image = building.image;
} else {
this.buildings.push(building);
}
});
}
}
The problem is that this does not work as expected, it always add new and new value it does not update:(