首页 > 解决方案 > 查询对象数组并设置值

问题描述

我有一个看起来像这样的对象数组:

[
    {
        "e_id": "1",
        "total": 0
    },
    {
        "e_id": "3",
        "total": 0
    }
]

在 forEach 循环中,我想total根据e_id. 我一直在尝试这样的事情:

e.forEach((row) => {
    this.arrayObj[row.e_id]['total']++;
});

但这不起作用,因为我认为它使用 e_id 作为索引而不是参考。

有没有办法可以工作?

标签: javascriptarraystypescript

解决方案


e=[
    {
        "e_id": "1",
        "total": 0
    },
    {
        "e_id": "3",
        "total": 0
    }
]

e.forEach((row) => {
    row['total']++;
});

你需要这个吗?


推荐阅读