jquery - Javascript update array value if exists otherwise push new array to object - Stack Overflow

I am trying to update a value in the array if it is found, if it isn't then add a new array to it.

I am trying to update a value in the array if it is found, if it isn't then add a new array to it.

Here's some code I have been trying:

var cartItems = {};
var items = []
cartItems.items = items;
$('.proAdd').click(function(){
var name = $(this).attr("data-name");
var price = parseFloat($(this).attr("data-price"));
var quantity = parseInt($(this).attr("data-quantity"));

var item = {"name": name,"price": price,"quantity": quantity}

items.forEach(function(item) {
if (item.name === name) {
item.quantity = 2
return; 
}else{
cartItems.items.push(item);
}
});

In this version noting gets pushed. If I take out the else branch then it does update it but also pushes it. I have created a fiddle for this.

Also tried this but it says x.quantity is not defined:

var index = items.findIndex(x => x.name==name)
if (index != -1){
x.quantity = 2
}
else {
cartItems.items.push(item);
}

I am trying to update a value in the array if it is found, if it isn't then add a new array to it.

Here's some code I have been trying:

var cartItems = {};
var items = []
cartItems.items = items;
$('.proAdd').click(function(){
var name = $(this).attr("data-name");
var price = parseFloat($(this).attr("data-price"));
var quantity = parseInt($(this).attr("data-quantity"));

var item = {"name": name,"price": price,"quantity": quantity}

items.forEach(function(item) {
if (item.name === name) {
item.quantity = 2
return; 
}else{
cartItems.items.push(item);
}
});

In this version noting gets pushed. If I take out the else branch then it does update it but also pushes it. I have created a fiddle for this.

Also tried this but it says x.quantity is not defined:

var index = items.findIndex(x => x.name==name)
if (index != -1){
x.quantity = 2
}
else {
cartItems.items.push(item);
}
Share asked Apr 25, 2019 at 21:42 Abu NoohAbu Nooh 8564 gold badges14 silver badges45 bronze badges 5
  • 1 items[index].quantity = 2 – Reactgular Commented Apr 25, 2019 at 21:48
  • 1 If name is unique shouldn't items be an object keyed to name? That would make this problem go away. – Mark Commented Apr 25, 2019 at 21:49
  • @cgTag that solved it, thank you! Could accept as answer would be appreciated if you could explain if I'd run into any browser patibility issues as well. – Abu Nooh Commented Apr 25, 2019 at 21:58
  • No browser issues here, and this was a syntax mistake so I'd rather not post an answer. – Reactgular Commented Apr 25, 2019 at 22:01
  • Possible duplicate of Array.push() if does not exist? – Striped Commented Apr 25, 2019 at 22:19
Add a ment  | 

1 Answer 1

Reset to default 6

Because index stores the index of an item, and x is a temporary value which is unavailable after that line. Use find instead, and make sure you're looking at the same items each time:

var item = cartItems.items.find(x => x.name == name);
if (item) {
  item.quantity = 2;
} else {
  cartItems.items.push(item);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744819272a4595516.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信