Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何计算键的值之和?_Javascript_Jquery_Collections_Underscore.js - Fatal编程技术网

Javascript 如何计算键的值之和?

Javascript 如何计算键的值之和?,javascript,jquery,collections,underscore.js,Javascript,Jquery,Collections,Underscore.js,我有这样的收藏: { "2.2": { "BALL":{"white":9,"black":11,"red":4}, "BAG":{"white":2,"black":11,"red":1}, "COVER":{"white":3,"black":8,"red":1} }, "2.3": { "BALL":{"white":1,"black":0,"red":7}, "BAG":{"whit

我有这样的收藏:

{
    "2.2": { 
        "BALL":{"white":9,"black":11,"red":4},
        "BAG":{"white":2,"black":11,"red":1},
        "COVER":{"white":3,"black":8,"red":1}
    },
    "2.3": {
        "BALL":{"white":1,"black":0,"red":7},
        "BAG":{"white":3,"black":0,"red":0},
        "COVER":{"white":9,"black":2,"red":28}
    }
}
{
    "2.2": {"white": 14, "black": 30, "red": 6},
    "2.3": {"white": 13, "black": 2, "red": 35}
}
我想计算“2.2”和“2.3”的总颜色(白、黑、红)

输出 所需的输出应如下所示:

{
    "2.2": { 
        "BALL":{"white":9,"black":11,"red":4},
        "BAG":{"white":2,"black":11,"red":1},
        "COVER":{"white":3,"black":8,"red":1}
    },
    "2.3": {
        "BALL":{"white":1,"black":0,"red":7},
        "BAG":{"white":3,"black":0,"red":0},
        "COVER":{"white":9,"black":2,"red":28}
    }
}
{
    "2.2": {"white": 14, "black": 30, "red": 6},
    "2.3": {"white": 13, "black": 2, "red": 35}
}
我尝试过使用
下划线
.map
函数,但无法正确理解它。任何人都可以帮忙? PS:需要仅使用下划线.js的解决方案。

尝试:

var obj = {
    "2.2": {
        "BALL": {
            "white": 9, "black": 11, "red": 4
        },
        "BAG": {
            "white": 2, "black": 11, "red": 1
        },
        "COVER": {
            "white": 3, "black": 8, "red": 1
        }
    },
    "2.3": {
        "BALL": {
            "white": 1, "black": 0, "red": 7
        },
        "BAG": {
            "white": 3, "black": 0, "red": 0
        },
        "COVER": {
            "white": 9, "black": 2, "red": 28
        }
    }
}
var w = 0;
for(var i in obj){
    for(var j in obj[i]){
        w +=obj[i][j]['white'];
    }
}
更新:

var obj = {
    "2.2": {
        "BALL": {
            "white": 9, "black": 11, "red": 4
        },
        "BAG": {
            "white": 2, "black": 11, "red": 1
        },
        "COVER": {
            "white": 3, "black": 8, "red": 1
        }
    },
    "2.3": {
        "BALL": {
            "white": 1, "black": 0, "red": 7
        },
        "BAG": {
            "white": 3, "black": 0, "red": 0
        },
        "COVER": {
            "white": 9, "black": 2, "red": 28
        }
    }
}
for (var i in obj) {
    var tmp = {
        white: 0,
        black: 0,
        red: 0
    };
    for (var j in obj[i]) {
        tmp['white'] += obj[i][j]['white'];
        tmp['black'] += obj[i][j]['black'];
        tmp['red'] += obj[i][j]['red'];
    }
    obj[i] = tmp
}
console.log(obj);

下面是
下划线.js
解决方案

_.mapObject(collections, function(collection) {
  return _.reduce(collection, function(memo, col) {
    // credit: Bergi on http://stackoverflow.com/a/17350790/1465828
    // the following line reads:
    // for (var p in col) => loop all properties in col
    // memo[p] = ... => assign memo[p] as ...
    // (p in memo ? memo[p] : 0) => IF property p exists in memo, then memo[p], otherwise 0
    // + col[p] => add col's p
    for (var p in col) { memo[p] = (p in memo ? memo[p] : 0) + col[p]; }
    return memo;
  }, {});
});

关于“2.2”和“2.3”,你所说的“关于”是什么意思?你想要两个和吗?一个简单的解决方案是在嵌套迭代中进行。2.2和2.3的外部迭代和内部迭代计算白色的总和…请检查编辑的问题。对不起,我没有很好地解释。你的解决办法是把所有的白色相加。请检查我提到的期望输出。Thanks@ahmadhamza已经进行了更新,以获得您稍后添加的输出。非常感谢。我已经将其标记为已接受答案,尽管它没有使用下划线.js方法。我使用
mapObject
函数得到类型错误。你知道为什么吗?请问你使用的是哪个版本的
\uuujs
?我可以在
map
中找到
mapObject
用于数组,而
mapObject
用于对象。最新版本是1.8.3,我查看了1.7.0源代码,发现还没有
mapObject
。所以,我的解决方案不适用于v1.7.0。备忘录中的
p是什么?