Reputation: 115
I have an array which is
var mycart = [{"id":"1","quantity":"10"},{"id":"6","quantity":"20"},{"id":"3","quantity":"30"},{"id":"4","quantity":"40"}];
//new id's which should be updated
var newid = "6";
var newquantity = "5";
Every-time on cart update i want to update the id and value,if id exists i need to update quantity if doesn't i have to create a new object of id,quantity. Now,i am adding newid which is 6,as you can see id=6 exist in array so it should just update like this -
for(var x=0; x < mycart.length; x++){
if(mycart[x].id == newid ){
var tmpq = mycart[x].quantity;
mycart[x].quantity = parseInt(tmpq) + parseInt(q);
}
else{
alert(mycart[x].id+' = this is not what you are searching');
}
}
now output is :
var mycart = [{"id":"1","quantity":"10"},{"id":"6","quantity":"25"},{"id":"3","quantity":"30"},{"id":"4","quantity":"40"}];
Now i need help to create new object,if that id doesn't exist in the mycart array.
//new id's which should be created var newid = "2"; var newquantity = "15"; var temparr = new Array(); var tmpobj ={}; tmpobj["id"]= newid ; tmpobj["quantity"] = newquantity ; temparr.push(tmpobj); mycart.push(temparr);
within the for loop it will create mycart.length
times,then i have to filter with $unique() jQuery API
which is bad method to follow.
All i need is to create new object with id:value,quantity:value
if it doesn't exists,if it exists then just update. Update part working,create part not able to,
Upvotes: 1
Views: 2004
Reputation: 16027
Since there is no reason for the array to contain multiple objects with the same id, I suggest you use an object as the root of the variable, using the id
as an array key (+ prefix to avoid confusions) :
var mycart = {"item_1":{"id":"1","quantity":"10"},"item_6":{"id":"6","quantity":"20"},...};
This way you can set/get quantity with mycart["item_"+id].quantity
so you can update it like that :
function updateQuantity(id, quantity){
if(!mycart["item_"+id]){
// create item in cart
mycart["item_"+id] = {"id":id};
}
// update quantity
mycart["item_"+id].quantity = quantity;
}
Upvotes: 0
Reputation: 362
What about something like this?
updateCart = function(newid,newquantity,cart){
for(var x=0; x < mycart.length; x++){
if(mycart[x].id == newid ){
var tmpq = cart[x].quantity;
cart[x].quantity = parseInt(tmpq) + parseInt(newquantity);
return;
}
else{
alert(mycart[x].id+' = this is not what you are searching');
}
}
cart.push({"id":newid,"quantity":newquantity});
return;
};
This avoids any 'flag' variables, which IMHO is nice. You could then call it like so:
updateCart("6","5",mycart); //[..{"id":"6","quantity":"25"}..]
updateCart("10","7",mycart); //[..{"id":"10","quantity":"7"}](new array member)
updateCart("1","5",mycart); //[{"id":"1","quantity":"15"}...]
I must agree with the others, though, when they say that you might want to use some sort of key so you don't have to iterate through the array each time.
Upvotes: 1
Reputation: 781088
Use a variable to detect if you found an old item:
var found = false;
for(var x=0; x < mycart.length; x++){
if(mycart[x].id == newid ){
var tmpq = mycart[x].quantity;
mycart[x].quantity = parseInt(tmpq) + parseInt(q);
found = true;
break;
}
}
if (!found) {
mycart.push ({ id: newid, quantity: newquantity });
}
It would be simpler if you made mycart
an object whose keys are the IDs, so you wouldn't have to do a linear search. It would be like this:
var mycart = {
"1": { quantity: 10 },
"6": { quantity: 20 },
...
};
Then your code would be:
if (mycart.hasOwnProperty(newid)) {
mycart[newid] += newquantity;
} else {
mycart[newid] = { quantity: newquantity };
}
Upvotes: 2
Reputation: 1540
To create a new item, I would do:
var match = false;
for (var x = 0; x < mycart.length; x++) {
var id = mycart[x].id;
//If there's a match, then item already exists
if (id === newid) {
//Update it
var tmpq = mycart[x].quantity;
mycart[x].quantity = parseInt(tmpq) + parseInt(q); //I'm not sure what q is, but I will leave it since you said it works in your code
match = true;
}
}
//If no match found, create new
if (!match) {
var newItem = {};
newItem.id = newid;
newItem.quantity = newquantity;
mycart.push(newItem);
}
Upvotes: 0