第一次使用购物车组件,我的代码结构如下。
StringRequest stringRequest=new StringRequest(Request.Method.GET, HI, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
JSONArray array=jsonObject.getJSONArray("data");
for (int i=0; i<array.length(); i++ ){
JSONObject ob=array.getJSONObject(i);
Notificacion listData=new Notificacion(ob.getString("id_notificacion")
,ob.getString("texto"),
ob.getString("fecha"),
ob.getString("nombre"),
ob.getString("nombre")+" "+ob.getString("apellidos"),
ob.getString("profesor"),
ob.getString("tutor"),
ob.getString("estado"),
ob.getString("envia_tutor"),
ob.getString("envia_profesor")
);
list_data.add(listData);
spinner.setVisibility(View.GONE);
}
rv.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("arry","array lengt error:"+error);
Toast.makeText(getActivity(),
"No tienes notificaciones.", Toast.LENGTH_LONG)
.show();
spinner.setVisibility(View.GONE);
}
});
RequestQueue requestQueue= Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
我基本上需要的是,如果我有多个所选尺寸,则shopItem.quantity会增加。
此刻我明白了。
let cartCount = document.getElementById("counter");
let isItemSelected = false;
let itemCount = 0;
let shoppingCart = [];
let selectedSize = "";
let displaySelectedSize = document.getElementById("selected");
let displayCart = document.getElementById("cart");
selectItem = () => {
isItemSelected = true;
selectedSize = event.srcElement.id;
displaySelectedSize.innerHTML = `${selectedSize}`;
}
addItem = () => {
if (isItemSelected === true) {
const shopItem = {
name: "Classic Tee",
price: 75,
size: `${selectedSize}`,
quantity: 0
}
itemCount+= 1;
cartCount.innerHTML = `( ${itemCount} )`;
shopItem.quantity++
shoppingCart.push(shopItem);
console.log(shoppingCart);
return itemSuccess();
} else {
return itemError();
}
}
我可以看到为什么我的输出是这样的,每次我的addItem函数被触发时,它都会创建一个新对象,但是如果有重复的话,我想改变我的shopItem.quantity ...
我该怎么做?
答案 0 :(得分:2)
您可以为项目时间指定一个身份属性(我可能不会用名字来命名,因为您可能会拥有同名的东西,这很不好),例如0x0
0x7ffeeef8ed88
,然后您可以搜索在添加东西之前。例如,考虑以下项目:
id
然后在添加项目之前,您可以在购物车中查找它:
const cartItem = {id: 1, name: "Classic Tee"}
答案 1 :(得分:1)
我认为您应该过滤shoppingCart数组,并根据它是否应该向该数组中添加新商品或增加现有商品的数量来进行过滤。
以下是根据您的开始而制作的示例。我删除了一些代码,以便可以专注于我的意思
let shoppingCart = [];
addItem = (newItem) => {
function itemFilter(item) {
return item.name === newItem.name && item.price === newItem.price && item.size === newItem.size
}
let existingItems = shoppingCart.filter(itemFilter)
if (existingItems.length > 0) {
existingItems[0].quantity += newItem.quantity
} else {
shoppingCart.push(newItem)
}
}
const shopItem = {
name: "Classic Tee",
price: 75,
size: "small",
quantity: 1
}
const shopItem2 = {
name: "Classic Tee",
price: 75,
size: "big",
quantity: 1
}
const shopItem3 = {
name: "Classic Tee",
price: 75,
size: "small",
quantity: 2
}
console.log('-----------')
console.log(shoppingCart)
addItem(shopItem)
console.log('-----------')
console.log(shoppingCart)
addItem(shopItem)
console.log('-----------')
console.log(shoppingCart)
addItem(shopItem2)
console.log('-----------')
console.log(shoppingCart)
addItem(shopItem)
console.log('-----------')
console.log(shoppingCart)
输出为:
-----------
[]
-----------
[ { name: 'Classic Tee', price: 75, size: 'small', quantity: 1 } ]
-----------
[ { name: 'Classic Tee', price: 75, size: 'small', quantity: 2 } ]
-----------
[ { name: 'Classic Tee', price: 75, size: 'small', quantity: 2 },
{ name: 'Classic Tee', price: 75, size: 'big', quantity: 1 } ]
-----------
[ { name: 'Classic Tee', price: 75, size: 'small', quantity: 4 },
{ name: 'Classic Tee', price: 75, size: 'big', quantity: 1 } ]
答案 2 :(得分:0)
您也可以像这样使用 reduce
:
const fakeGiftCardList2 = [
{ value: 8, quantity: 1, cost: 3 },
{ value: 8, quantity: 1, cost: 3 },
{ value: 8, quantity: 1, cost: 3 },
{ value: 8, quantity: 1, cost: 3 },
{ value: 8, quantity: 1, cost: 3 },
{ value: 10, quantity: 1, cost: 12 },
];
const red = (obj, item) => {
obj[item.value]
? (obj[item.value].quantity += 1)
: (obj[item.value] = { ...item });
return obj;
};
const arrayHashmap = fakeGiftCardList2.reduce(red, {});
const mergedArray = Object.values(arrayHashmap);
console.log(mergedArray);