I am facing an issue with the function findPrice where I need to call it multiple times to delete objects that match a specific price. The problem arises when dealing with large maps and two functions.
Here is the JSON format:
[ { _id: 5c6c408dbec3ab457cf5bdfb,
date: 2019-02-19T00:00:00.000Z,
user: 5c30fd5890bbd24a1c46c7ee,
positionDetail: [ { quantity: 2, price:22}, { quantity: 3, price:33}, { quantity: 5, price:43}],
id: 50,
__v: 0 },
{ _id: 5c6c408dbec3ab457cf5bdfb,
date: 2019-02-27T00:00:00.000Z,
user: 5c30fd5890bbd24a1c46c7ee,
positionDetail: [ { quantity: 3, price:33}, { quantity: 2, price:123}, { quantity: 2, price:11}],
id: 51,
__v: 0 }, ]
The issue seems to be related to the index of the map. I attempted to decrement 'i' when deleting an object from the map, but it did not yield the expected results.
findPrice() {
this.arrayOfObjects.forEach( (data, i) => {
let searchIfExist= data.positionsDetail.findIndex(index1 => index1.price === priceSearch);
if (searchIfExist === -1) {
this.arrayOfObjects.splice(i, 1);
i--;
}
});
}