简体   繁体   English

在 object 中查找以编辑或添加

[英]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.

相关问题 在数组中查找对象,然后对其进行编辑 - Find object in array and then edit it 添加对象或编辑(如果存在)(indexeddb) - add an object or edit if exist (indexeddb) 如何在对象数组中查找和编辑对象的属性? - How to find and edit property of object in the array of an objects? 如何在数组对象中找到ID,然后在oop JavaScript中编辑和删除对象? - How to find id in array object then edit and delete a object in oop javascript? 使用相同的引导程序模态添加新对象并编辑该对象 - Use the same bootstrap modal to both add a new object and edit that object 使用 Jquery find() 编辑表,然后使用 before() 添加包含数组数据的单元格 - Edit a table with Jquery find() then add cell with data from an array with before() 在数组中深度查找 object 并编辑然后放回原处 - Deep find object in array and edit then put back in place Rails 3.2和汞编辑器无法通过编辑/路由找到对象 - rails 3.2 and mercury editor cant find object through edit/ route 如何在嵌套的 object 数组上添加属性并编辑原始数组? - how to add property on nested object array and edit the original array? 将“另存为”按钮添加到Django异物编辑弹出窗口 - Add “Save as new” button to Django foreign object edit popup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM