I'm in the process of developing a stock trading application, and I have an empty array to store the stocks that are bought. Each time a stock is purchased, it adds an object to the array. If the same stock is bought again, I want to check for that specific stock in the array and only update its quantity. Otherwise, I want to add a new object to the array.
I've attempted to use a forEach loop and some functions, but I'm struggling to identify the current element in the array that has already been bought so I can update its quantity. I know how to calculate the quantity to be added, but I can't pinpoint the specific object to append it to.
The 'item' variable represents the object that needs to be appended to the array, adding only the quantity if the object is already present in the array. Thanks!
stocks = [
{id:0, name:'BMW', price:5, quantity:0},
{id:1, name:'Google', price:20, quantity:0},
{id:2, name:'IBM', price:34, quantity:0},
{id:3, name:'Apple', price:15, quantity:0}
];
...
...
stockPortfolio = []
...
//the item is
'ADD_PORTFOLIO_ITEM'(stockPortfolio , item){
//check if item id already exists
// if not, create a new one
const arrayP = stockPortfolio;
const found = arrayP.some(el => el.name === item.name);
if (found) {
}
else{