简体   繁体   English

如何使用动态键获取数组 object 值的总值

[英]how to get total value of array object values using dynamic keys

I have the following mock array,我有以下模拟数组,

this array is demo purpose, owlCount doesnt make sense i know.这个数组是演示目的,我知道 owlCount 没有意义。

let arr = [
    {
        "id": "000701",
        "status": "No Source Info",
        "sources": []
    },
    {
        "id": "200101",
        "status": "Good",
        "sources": [
            {
                "center": "H2",
                "uri": "237.0.1.133",
                "owlCount": 1,
                "status": "Good",
                "state": {
                    "authState": "authorized",
                    "lockState": "locked"
                }
            }
        ]
    },
    {
        "id": "005306",
        "status": "Good",
        "sources": [
            {
                "center": "H1",
                "uri": "237.0.6.5",
                "owlCount": 3,
                "status": "Good",
                "state": {
                    "authState": "authorized",
                    "lockState": "locked"
                }
            },
            {
                "center": "H1",
                "uri": "237.0.6.25",
                "owlCount": 5,
                "status": "Good",
                "state": {
                    "authState": "authorized",
                    "lockState": "locked"
                }
            }
        ]
    }
]

How would i go about using reduce to add the values owlCount within each nested array.我将如何 go 关于使用 reduce 在每个嵌套数组中添加值owlCount Without doing [0] to get within the nested array无需执行[0]即可进入嵌套数组

I was thinking of something like this, but i get the value of 0, when it should be 9我在想这样的事情,但我得到的值为 0,而它应该是 9

const sum = arr.reduce( (acc, cv, i) => {
    acc[i] += cv.owlCount
return acc
}, 0)

What am i doing wrong, and what should be the solution.我做错了什么,应该有什么解决方案。

here acc is a number not an array, not sure why you would use acc[i]?这里的acc是一个数字而不是一个数组,不知道你为什么要使用 acc[i]? You will have to run two reduce loops here.您必须在这里运行两个 reduce 循环。 One for outer array and one for inner array to get the sum of owlCount from sources.一个用于外部数组,一个用于内部数组,以从源中获取 owlCount 的总和。

 let arr = [ { "id": "000701", "status": "No Source Info", "sources": [] }, { "id": "200101", "status": "Good", "sources": [ { "center": "H2", "uri": "237.0.1.133", "owlCount": 1, "status": "Good", "state": { "authState": "authorized", "lockState": "locked" } } ] }, { "id": "005306", "status": "Good", "sources": [ { "center": "H1", "uri": "237.0.6.5", "owlCount": 3, "status": "Good", "state": { "authState": "authorized", "lockState": "locked" } }, { "center": "H1", "uri": "237.0.6.25", "owlCount": 5, "status": "Good", "state": { "authState": "authorized", "lockState": "locked" } } ] } ] const sum = arr.reduce( (acc, item) => { return acc += item.sources.reduce((a,source) => a += source.owlCount,0) }, 0) console.log(sum);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM