簡體   English   中英

如何在對象數組中使用相同的鍵對值求和?

[英]How to sum values with the same key In an array of objects?

假設我有一個像這樣的對象數組:

this.arrayOfObjects = [{
    {
        "grouppresentation": "11",
        "evaluation": null,
        "datacontext": null,
        "category": "Group Presentation"
    },
    {
        "grouppresentation": null,
        "evaluation": "23",
        "datacontext": null,
        "category": "Evaluation"
    },
    {
        "grouppresentation": null,
        "evaluation": "46",
        "datacontext": null,
        "category": "Evaluation"
    },
    {
        "grouppresentation": "44",
        "evaluation": null,
        "datacontext": null,
        "category": "Group Presentation"
    },
    {
        "grouppresentation": null,
        "evaluation": null,
        "datacontext": "21",
        "category": "Data Context"
    }
}]

每個 object 中的鍵由“類別”鍵中的值定義。 這意味着如果“category”=“Group Presentation”,則存在一個名為“grouppresentation”的鍵,其中“category”值已使用 replace(' ', '').toLowerCase() 轉換。

它沒有在上面顯示,但是有一個 object 帶有“category”=“Data Context”,所以在同一個 object 中有一個名為“datacontext”的鍵。

另一個重要注意事項是,如果“category”=“Group Presentation”,則“grouppresentation”鍵將有一個值,即一個數字。 其他鍵將設置為 null,因此唯一具有實際值的鍵是“category”和“grouppresentation”。 像這樣,但類別為“評估”:

    {
        "grouppresentation": null,
        "evaluation": "46",
        "datacontext": null,
        "category": "Evaluation"
    },

目標

我想要每個具有相同“類別”值的項目,對表示該值的鍵的值求和:

所以,

this.newArrayOfObjects = [{
    {
        "grouppresentation": "55",     -> because 11 + 44 from the values above
        "evaluation": null,
        "datacontext": null,
        "category": "Group Presentation"
    },
    {
        "grouppresentation": null,
        "evaluation": "69",    -> because 46 + 23 from the values above
        "datacontext": null,
        "category": "Evaluation"
    },
    {
        "grouppresentation": null,
        "evaluation": null,
        "datacontext": "21",
        "category": "Data Context"
    }
}]

到目前為止,我已經嘗試了幾種方法,其中之一是:

var newObject = {};

this.arrayOfObjects.forEach(function(item) {
  if (newObject.hasOwnProperty(item.category)) {
    newObject[item.name] = newObject[item.category.replace(' ', '').toLowerCase()] + item.category.replace(' ', '').toLowerCase();
  } else {
    newObject[item.name] = item.category.replace(' ', '').toLowerCase();
  }
});

然而,不幸的是,它沒有得到任何結果並產生以下結果:

{undefined: 'grouppresentation'}

您知道如何實現上述目標嗎?

Array#reduce是到達那里的一種方式。 基本上,我們只是檢查每個迭代是否與類別匹配。 如果匹配,我們將適當的值加在一起(即時將兩者轉換為數字)

 let arrayOfObjects = [{ "grouppresentation": "11", "evaluation": null, "datacontext": null, "category": "Group Presentation" }, { "grouppresentation": null, "evaluation": "23", "datacontext": null, "category": "Evaluation" }, { "grouppresentation": null, "evaluation": "46", "datacontext": null, "category": "Evaluation" }, { "grouppresentation": "44", "evaluation": null, "datacontext": null, "category": "Group Presentation" }, { "grouppresentation": null, "evaluation": null, "datacontext": "21", "category": "Data Context" } ] let newArrayOfObjects = arrayOfObjects.reduce((b, a) => { let ind = b.findIndex(e => e.category === a.category); let c = a.category.toLowerCase().split(' ').join(''); if (ind > -1) { b[ind][c] = +b[ind][c] + +a[c] } else { a[c] = +a[c] || 0 b.push(a) } return b; }, []); console.log(newArrayOfObjects)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM