I am looking to insert data into an array list based on the product id. If the product id already exists in the array list, I need to display an alert message; otherwise, I want to add it to the list. Please provide me with the necessary conditions for this.
public stockDataSource: Array<any> = [];
const stockData: StockTrackingItem = {
StockTrackingItemId: 0,
StockTrackingId: 0,
Sno: this.Sno,
ProductId: stock.productId,
SKU: stock.sku,
SKUId: stock.skuId
};
if (this.stockDataSource.length === 0) {
this.stockDataSource.push(stockData);
} else {
for (let index = 0; index < this.stockDataSource.length; index++) {
if (this.stockDataSource[index].ProductId === stockData.ProductId) {
this.alertService.warnAlert("Product already exists");
break;
}
}
this.stockDataSource.push(stockData);
}