提问人:Safina Siddique 提问时间:8/26/2021 更新时间:8/26/2021 访问量:737
如何检查动态数组中是否存在项目?
How to check if an item exist in a dynamic array?
问:
我想将 Items 添加到 cartArray。如果 Array 已经有一个项目,我将编写一个函数,通过增加其数量来防止它再次添加它。这怎么可能。数组项有多个元素(例如 id、price、quantity)。
$(document).ready(function(){
var cartArray = []; //array of cart Items data
var total = 0;
var shipping = 2;
$(".addToCart").click(function(){
var flag = 0;
//Get all data of item to be added
var id= $(this).data("id");
var price= $(this).data("price");
var image = $(this).data('image');
var name = $(this).data("name");
var quantity = $(this).data("quantity");
var availability = $(this).data("availability");
var weight = $(this).data("weight");
//check if cart is empty to remove empty cart view
if( (cartArray.length === 0) ) {
$("#empty-cart").css("display","none");}
//Traverse the cart list to check if item already exists or not
for(var i = 0; i < cartArray.length; ++i){
if (//what to do here to match id){
var increment= $( this ).find(".quantity").val();
increment++;
flag = 1;
$( this ).find(".quantity").val(increment);
return false;
}
}
//Add new Item in cart
if(flag == 0){
var cartItem = "<li class='newItem'><p class='id'>"+id+"</p><div class='picture'><img src='"+image+"' ></div> <div class='details'><p class='name'>"+name+"</p><span class='weight'>"+weight+"</span><span> /</span> <span class='avail'>"+availability+"</span><p class='price'>"+price+"</p><a type='button' class='inc'>+</a><input type='text' class='quantity' value="+quantity+" ><a type='button' class='dec'>-</a></div> <a type='button' class='removeItem'><i class='fa fa-trash'></i></a></li>"
cartArray.push(cartItem);
localStorage.setItem('cartItem',JSON.stringify(cartArray));
var getItems = JSON.parse(localStorage.getItem('cartItem'));
// $("#cartList").prepend(cartItem);
$("#cartList li .id").css('display','none');
console.log("Item Added")
//increment in badge icon number when new item added in cart
var counterInc = parseInt($(".badge").html()) + 1;
$(".badge").html(counterInc);
}
})
答:
0赞
alexkarpen
8/26/2021
#1
编辑
在亚历山大的评论之后,您必须修改解决方案
function isInCart(id){
var items = JSON.parse(localStorage.getItem('actualCart'));
return !!items.filter(i=>i.id == id).length;
}
$(document).ready(function(){
var actualCartArray = []; //array of cart Items data
var cartArray = []; //array of cart Items html
var total = 0;
var shipping = 2;
$(".addToCart").click(function(){
if(isInCart($(this).data("id"))){
return;
}
var flag = 0;
//Get all data of item to be added
var id= $(this).data("id");
var price= $(this).data("price");
var image = $(this).data('image');
var name = $(this).data("name");
var quantity = $(this).data("quantity");
var availability = $(this).data("availability");
var weight = $(this).data("weight");
actualCartArray.push({id,price,image,name,quantity,availability,weight});
//check if cart is empty to remove empty cart view
if( (cartArray.length === 0) ) {
$("#empty-cart").css("display","none");}
//Traverse the cart list to check if item already exists or not
for(var i = 0; i < cartArray.length; ++i){
if (//what to do here to match id){
var increment= $( this ).find(".quantity").val();
increment++;
flag = 1;
$( this ).find(".quantity").val(increment);
return false;
}
}
//Add new Item in cart
if(flag == 0){
var cartItem = "<li class='newItem'><p class='id'>"+id+"</p><div class='picture'><img src='"+image+"' ></div> <div class='details'><p class='name'>"+name+"</p><span class='weight'>"+weight+"</span><span> /</span> <span class='avail'>"+availability+"</span><p class='price'>"+price+"</p><a type='button' class='inc'>+</a><input type='text' class='quantity' value="+quantity+" ><a type='button' class='dec'>-</a></div> <a type='button' class='removeItem'><i class='fa fa-trash'></i></a></li>"
cartArray.push(cartItem);
localStorage.setItem('cartItem',JSON.stringify(cartArray));
localStorage.setItem('actualCart',JSON.stringify(actualCartArray));
var getItems = JSON.parse(localStorage.getItem('cartItem'));
// $("#cartList").prepend(cartItem);
$("#cartList li .id").css('display','none');
console.log("Item Added")
//increment in badge icon number when new item added in cart
var counterInc = parseInt($(".badge").html()) + 1;
$(".badge").html(counterInc);
}
})
创建并使用函数,如下所示。如果项目是否在 localStorage 购物车中,则传递一个 id 并返回布尔值。
function isInCart(id){
var items = JSON.parse(localStorage.getItem('cartItem'));
return !!items.filter(i=>i.id == id).length;
}
评论
1赞
Alexander Nied
8/26/2021
如果他们将数据对象存储在数组中并将它们字符串化以存储在 localStorage 中,这将是一个很好的解决方案——但是,如果您查看下面的条件,您会看到他们存储的实际上是他们打算在 UI 中呈现的标记字符串。//Add new Item in cart
评论