JS Split ( ) to check if substring exists in Array
Posted
by
Javacadabra
on Stack Overflow
See other posts from Stack Overflow
or by Javacadabra
Published on 2014-06-10T08:27:00Z
Indexed on
2014/06/10
9:25 UTC
Read the original article
Hit count: 136
I have an array
of products that are stored as Strings
in this format productname:quantity
. The issue I am running into is that if a user adds one product with a quantity of x it is inserted into the array
as it should. However, if they then decide to add more of a particular product a new entry is made into the array
instead of checking if the product already exists and adjusting the quantity to the new value. oldQty + newQty
.
For example this is my array:
["CBL202659/A:1","OUTER9:1","PALLET CARDS:1"]
If I add another PALLET CARDS product it creates a new entry rather than updating the quantity of the existing item to 2.
New array
["CBL202659/A:1","OUTER9:1","PALLET CARDS:1","PALLET CARDS:1"]
I would like the array to end up like this: - updating the quantity
["CBL202659/A:1","OUTER9:1","PALLET CARDS:2"]
Currently this is my code:
I use the split()
method to seperate the String where a colon occurs and store the product name and quantity in two seperate variables.
$(".orderBtn").click(function(event){
//Show the order Box
$(".order-alert").show();
event.preventDefault();
//Create the Array
var productArray = [];
//Get reference to the product clicked
var stockCode = $(this).closest('li').find('.stock_code').html();
//Get reference to the quantity selected
var quantity = $(this).closest('li').find('.order_amount').val();
var item = stockCode + ":" + quantity;
var itemCheck = stockCode + ":";
if(quantity == 0){
console.log("Quantity must be greater than 0")
}else{
//If no Cookie exists, create one and add the Array
if ($.cookie('order_cookie') === undefined) {
console.log("CREATE NEW COOKIE");
//Add items to Array
productArray.push(item);
//Add Array to Cookie
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
//If the Cookie already exists do this
} else {
productArray = JSON.parse($.cookie('order_cookie'));//get ref to array
if(productArray.indexOf(itemCheck)!= -1){//It exists so update qty
console.log("EXISTS... updating item: " + itemCheck);
//var index = productArray.indexOf(item);
//var update = productArray[index].split(":");
//var name = update[0];
//var oldQty = update[1];
//console.log(name + ":" + oldQty);
//productArray[index] = item;
}else{//It does not exist, so add to array
console.log("Does not exist... adding new item: " + item);
//Append items onto the Array
productArray.push(item);
}
//Update the Cookie
$.cookie('order_cookie', JSON.stringify(productArray), { expires: 1, path: '/' });
console.log($.cookie('order_cookie'));
}
//Display the number of items in the Array in the Order Box
$('#order_counter').html(productArray.length);
}
});
I suppose the real question I am asking here, is if it is possible to search the array
for a subString
- containing productname:
??
© Stack Overflow or respective owner