Let's say I have an array.
[ '219_1_undefined', '244_1_undefined', '248_1_20///179+25///153', '221_6_undefined' ]
Breakdown for a single item in the array goes by 219_1_undefined basically is 219 is product_id, 1 is the quantity and undefined is the product_options I'm storing these information in local storage. Now I have a new item that needs to be added to the cart, but this product is already in cart so I want to add the quantity alone (middle value). So I want to add 219_1_undefined again to the existing array. Now the array will be look like this.
[ '219_1_undefined', '244_1_undefined', '248_1_20///179+25///153', '221_6_undefined', '219_1_undefined' ]
My problem is I don't know how to add the middle value(quantity) of the same product and get result like
[ '244_1_undefined', '248_1_20///179+25///153', '221_6_undefined', '219_2_undefined' ]
Below is the code I tried
let pid = 219;
let qty = 1;
let params = "undefined";
let string = '219_1_undefined|244|1_undefined|248_1_20///179+25///153|221_1_undefined';
let items = string.split('|');
let nItem = pid+'_'+qty+'_'+params;
let mx = items;
mx = mx.filter(function (item) {
return item.indexOf(pid+'_') !== 0;
});
mx.push(nItem);
var result = mx.toLocaleString();
let nresult = result.split(',').join('|');
console.log(nresult);
No matter what I do I still get the wrong output.
{ id: ..., options: ..., quantity: ... })?JSON.parse()so throw in an additionalJSON.stringify()and work with something more useful (like an array of objects)