我想检查数组并找到包含某个东西的数组
我有一个包含这些值的cartarray
{id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"}
{id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"}
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "3"}
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"}
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"}
每个对象代表一个产品 我真正想做的是防止重复id,所以如果id是相同的,我想巩固数量。所以在我添加这种格式的新产品对象之前
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "7"}
我想检查一下cartarray中是否有类似的产品
var arrayLength = cartarry.length;
for (var i = 0; i < arrayLength; i++) {
if (cartarry[i] == product.id ){
console.log("we got a match")
var updatedquantity = quantity + parseInt(product.quantity)
}
}
我尝试了几种不同的方法但不成功。如何找到匹配的ID并更新数量?我希望我的描述清楚
答案 0 :(得分:1)
我会解决这个问题。这条线
getUser
应该是
if (cartarray[i] == product.id) {
此外,如果只有零个或一个与product.id匹配的cartarray条目,请考虑在累加器之后暂停。
答案 1 :(得分:1)
要按ID使用Array.prototype.reduce合并产品数量:
let data = [
{id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"},
{id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"},
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "3"},
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"},
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"}
];
data = data.reduce((acc, item) => {
const foundItem = acc.find(_item => _item.id === item.id);
if(foundItem) {
foundItem.quantity = Number(item.quantity) + Number(foundItem.quantity) + '';
}
else {
acc.push(item);
}
return acc;
}, []);
console.log(data);
//0: {id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "2"}
//1: {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "5"}
<强> UPD 即可。借助reduce
方法,我们希望获得原始数据数组的处理副本。输出数组形成为acc
累加器值,最初是一个空数组(由reduce
的最后一个参数调节:[]
)。初始数据集的每个项目在回调中被单独考虑为item
局部变量,我们正在根据当前acc
内容更改item
。我们正在尝试通过item
查找当前acc
数组中的当前id
以合并数量。否则,如果当前item
数组的当前acc
是唯一的,我们会将当前item
推送到acc
。
答案 2 :(得分:0)
t:([] td:2001.01.01 2001.01.02 2001.01.03 2001.01.04 2001.01.05 2001.01.06;
AA:121.5 125.0 127.0 126.0 129.2 130.0;
BB:111.0 115.3 117.0 116.0 119.2 120.0;
wAA: 0.2 0.2 0.3 0.4 0.5 0.9;
wBB: 0.8 0.8 0.7 0.6 0.5 0.1;
fv:100000 0N 0n 0n 0n 0n;
sAA: 0n;
sBB: 0n)
t
kt:`td xkey t
// Calculate the first row of shares owned
kt:update sAA:fv*wAA%AA, sBB:fv*wBB%BB from kt
kt
// Global variables for previous shares owned
gPrevSAA:1.0
gPrevSBB:1.0
// Function to calclate the FV. If any of the previous shares owned paraeters are
// null then use the global parameters.
calcFV:{[fv;pSAA;pxA;pSBB;pxB]
// The first time calcFV is called, pSAA will be defined. The remainder it will be null.
$[pSAA=0n;pSAA:gPrevSAA;pSAA:pSAA];
$[pSBB=0n;pSBB:gPrevSBB;pSBB:pSBB];
// Calculate the fund value
uFV:-1;
$[fv=0n;uFV:(pSAA*pxA)+(pSBB*pxB);uFV:fv];
// update global values
$[pSAA<>0n;`gPrevSAA set pSAA;];
$[pSBB<>0n;`gPrevSBB set pSBB;];
uFV
}
// Calculate the fund values
kt:update fv:calcFV ' [fv;prev sAA;AA;prev sBB;BB] from kt
// Update the shares owned columns with the newly calcualted fund values
kt:update sAA:fv*wAA%AA, sBB:fv*wBB%BB from kt
您的帖子中没有大量信息,所以我猜了几件事。关键细节是:
var arrayLength = cartarry.length;
var match = false;
for (var i = 0; i < arrayLength; i++) {
if (cartarry[i].id == product.id ) {
console.log("we got a match")
cartarry[i].quantity += parseInt(product.quantity);
match = true;
break; // because this can only happen once per array
}
}
if (!match) {
// add it to the array
}
与product.id
进行比较。 cartarry[i]
包含一个包含字段cartarry[i]
,id
,name
和price
的对象。您必须使用这些字段进行比较,以及更新。答案 3 :(得分:0)
尝试这种方法:
var grouped = [];
var array = [{id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"},
{id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"},
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "3"},
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"},
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"}]
array.forEach(function (o) {
if (!this[o.id]) {
this[o.id] = { id: o.id, name:o.name, price:o.price, quantity: 0 };
grouped.push(this[o.id]);
}
this[o.id].quantity += parseInt(o.quantity);
}, Object.create(null));
console.log(grouped);