I have an array of orders and when i recieve a message from my websocket with a new placement order or a modified order i want to check against my orders array, if there is an exisitng order with the websocket message replace the object in the array with the websocket message, else push it to the array.
example:
const orderArr = [
{
id: 1,
item: 'apple',
price: 20
},
{
id: 2,
item: 'mango',
price: 10
},
{
id: 3,
item: 'cucumber',
price: 300
}
]
const webSocketOrder = {
id: 1,
item: 'apple',
price: 40
}
// what should happen to the order array
[
{
id: 1,
item: 'apple',
price: 40
},
{
id: 2,
item: 'mango',
price: 10
},
{
id: 3,
item: 'cucumber',
price: 300
}
]
but if the webSocketOrder is a new item with a new id it should be added as a new item in the orderArr
what i have done
const foundOrder = orderArr.find(
(x) => x.id === webSocketOrder.id
);
if (foundOrder) {
orderArr.map((ord) =>
ord.id === webSocketOrder.id
? webSocketOrder
: ord
);
} else {
orderArr.unshift(webSocketOrder);
}
this doesnt work for some reason, please can someone help?
orderArr( (x) => ..)