I’m getting duplicate values in JSON. I want to replace the previous value with new a value.
var obj = {};
obj.Product_Id = product_id;
obj.Qty = j;
var arr = [];
arr.push(obj);
var json = JSON.stringify(arr);
output is [{“Product_id”:1,”Qty”:1},{“Product_id”:2,”Qty”:1},{“Product_id”:1,”Qty”:2}]
i want this to be [{“Product_id”:1,”Qty”:2},{“Product_id”:2,”Qty”:1}]
If key repeat its value it should replace previous one not add the duplicate value
If you want to maintain a unique key, you must remove any existing element with this key and then add the new object
If you want to maintain the order and know an element with this key exists already, you can use
mapFor a reference on Array methods and properties, you can look at
Array.