Javascript forEach Example

I have a Javascript array with products and its name is product_table. Product is Javascript object type and it will have following look

So product_table will have different types of products. So i can add new product to product_table using push method

product_table.push( )

How do you prevent adding duplicate products to product_table instead you should increase the qty by one

This is how you solve:

You need to iterate the product_table array and check whether the items which you going to add exists

To do this you can use the forEach() method of the array. When you iterate the array you can use if condition to check