[英]Find in object to Edit or Add
I have an object productCounts
我有一个productCounts
[{provisioned=2.0, product=str1, totalID=1.0},
{product=str2, provisioned=4.0, totalID=3.0},
{provisioned=6.0, product=str3, totalID=5.0}]
I have an array uniqueProduct
我有一个数组uniqueProduct
[str1, str2, str3, str4]
I am then looping a dataset to get the totalID count, add it to the product's totalID but if it doesn't exist, push it to the object.然后我循环数据集以获取 totalID 计数,将其添加到产品的 totalID,但如果它不存在,则将其推送到 object。
var countID = 0;
uniqueProduct.forEach(
currentproduct => {
countID = 0;
for (var i = 0; i < shtRng.length; ++i) {
if (shtRng[i][ProductCol].toString() == currentproduct) { // && shtRng[i][IDcol].toString().length>4){
countID++;
}
}
if (countID == 0) {
return;
}
console.log(currentproduct + ": " + countID);
}
)
This works perfectly to return the countID per product in uniqueProduct
这非常适合在 uniqueProduct 中返回每个产品的uniqueProduct
Rather than logging the result, I would like to add it to the object like this... If the current unique product is not in the productCounts
object, add it.而不是记录结果,我想像这样将它添加到 object... 如果当前唯一产品不在productCounts
中,请添加它。
let obj = productCounts.find((o, i) => {
if (o.product == currentproduct) {
productCounts[i] = { product: currentproduct, totalID: productCounts[i].totalID+countID, provisioned: productCounts[i].provisioned };
return true;
} else {
productCounts.push({ product: currentproduct, totalID: countID, provisioned: 0 });
return true;
}
});
In my head, this should work but it appears to skip some records or add the product multiple times.在我看来,这应该可以,但它似乎会跳过一些记录或多次添加产品。 How do I add to the object correctly?如何正确添加到 object?
Expected output is the object to be something similar to:预计 output 是 object 类似于:
[{provisioned=2.0, product=str1, totalID=35.0}, {product=str2, provisioned=4.0, totalID=8.0}, {provisioned=6.0, product=str3, totalID=51.0}, {provisioned=6.0, product=str4, totalID=14.0}] [{provisioned=2.0, product=str1, totalID=35.0}, {product=str2, provisioned=4.0, totalID=8.0}, {provisioned=6.0, product=str3, totalID=51.0}, {provisioned=6.0, product= str4,totalID=14.0}]
The argument to find()
is a function that returns a boolean when the element matches the criteria. find()
的参数是 function,当元素匹配条件时返回 boolean。 The if
statement should use the result of this, it shouldn't be in the condition function. if
语句应该使用 this 的结果,它不应该在条件 function 中。
let obj = productCounts.find(o => o.product == currentProduct);
if (obj) {
obj.totalId += countID;
} else {
productCounts.push(productCounts.push({ product: currentproduct, totalID: countID, provisioned: 0 });
}
BTW, your life would be easier if you used an object whose keys are the product names, rather than an array of objects.顺便说一句,如果您使用 object 其键是产品名称而不是对象数组,您的生活会更轻松。 You can easily turn the array of objects into such an object:您可以轻松地将对象数组变成这样的 object:
let productCountsObj = Object.fromEntries(productCounts.map(o => [o.product, o]));
if (currentProduct in productCountsObj) {
productCountsObj[currentProduct].totalID += countID;
} else {
productCountsObj[currentProduct] = { product: currentproduct, totalID: countID, provisioned: 0 };
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.