我正在与ReactJS合作.我有一个如下所示的JSON对象:
[
{
"id": "1",
"category": "cellphones",
"name": "Xiaomi Redmi Note 10",
"stock": "99",
"price": "100",
"quantity": "3"
},
{
"id": "2",
"category": "cellphones",
"name": "Iphone 11",
"stock": "3",
"price": "1000",
"quantity": "5"
},
{
"id": "3",
"category": "laptops",
"name": "MSI GF65 Thin",
"stock": "40",
"price": "2000",
"quantity": "2"
}
]
我想通过这样做来获得这些项目的总体总数:
const getAllTotal = () => {
const sum = cartItems.reduce(
(prev, next) => prev.quantity * prev.price + next.quantity * next.price,
0
);
console.log(sum);
return sum;
};
问题是,每次我调用该函数时,控制台都返回NaN.
我首先认为这是由键的类型引起的,所以我try 将这些键包含在parseInt()方法中,但它返回相同的NaN.我做错了什么?