简体   繁体   English

使用JavaScript在二维数组中添加值

[英]Add values in a two-dimensional array with JavaScript

I want to merge and add the following 2D array: 我想合并并添加以下2D数组:

var arr = [
    ["TWENTY", 20],
    ["TWENTY", 20],
    ["TWENTY", 20],
    ["TWENTY", 20],
    ["TEN", 10],
    ["FIVE", 5],
    ["ONE", 1],
    ["QUARTER", 0.25],
    ["QUARTER", 0.25],
    ["DIME", 0.1],
    ["DIME", 0.1],
    ["PENNY", 0.01],
    ["PENNY", 0.01],
    ["PENNY", 0.01],
    ["PENNY", 0.01]
    ];

Resulting in a 2D array that looks like this: 导致2D数组看起来像这样:

 arr = [
     ["TWENTY", 80],
     ["TEN", 10],
     ["FIVE", 5],
     ["QUARTER", 0.50],
     ["DIME", 0.20],
     ["PENNY", 0.04]
     ];

Basically I'm adding up the number values and condensing the string values. 基本上我将数值加起来并压缩字符串值。 I tried map, reduce, and for loops but this one has me stumped. 我试过map,reduce和for循环,但这个让我难过。

You have an array with key, value tuples. 你有一个包含key, value元组的数组。 You can iterate over these tuples, store each new key in an object, and increment the value as you come upon subsequent tuples the same key. 您可以遍历这些元组,将每个新密钥存储在一个对象中,并在后续元组使用相同的密钥时增加该值。 If you keep an array of keys in the order in which you encountered them, you can map the array of keys to a result array. 如果按照遇到它们的顺序保留一组键,则可以将键数组映射到结果数组。

But it would be foolish to go to all this trouble when you could use the purely functional approach shown in Nit's answer. 但是当你可以使用Nit的答案中所示的纯功能方法时,解决所有这些麻烦是愚蠢的。

Below is the ugly approach. 以下是丑陋的方法。 Don't do this. 不要这样做。 Do what Nit says. 做什么Nit说。

 function addArray(tuples) { var hash = {}, keys = []; tuples.forEach(function (tuple) { // Iterate over [key, value] tuples. var key = tuple[0], value = tuple[1]; if (hash[key] === undefined) { // Is it a new key? keys.push(key); // Then add it to keys. hash[key] = value; // And set the initial value. } else { hash[key] += value; // Otherwise, increment the value. } }); return keys.map(function (key) { // Map the keys to tuples. return([key, hash[key]]); }); }; var arr = [ ["TWENTY", 20], ["TWENTY", 20], ["TWENTY", 20], ["TWENTY", 20], ["TEN", 10], ["FIVE", 5], ["ONE", 1], ["QUARTER", 0.25], ["QUARTER", 0.25], ["DIME", 0.1], ["DIME", 0.1], ["PENNY", 0.01], ["PENNY", 0.01], ["PENNY", 0.01], ["PENNY", 0.01] ]; var added = addArray(arr); added.forEach(function (tuple) { document.write(JSON.stringify(tuple) + '<br />'); }); 

All you really need is a reduce and a map, which could probably add up to a good MapReduce joke. 你真正需要的只是一个简化和一个地图,这可能会加起来很好的MapReduce笑话。

var arr = [
    ["TWENTY", 20],
    ["TWENTY", 20],
    ["TWENTY", 20],
    ["TWENTY", 20],
    ["TEN", 10],
    ["FIVE", 5],
    ["ONE", 1],
    ["QUARTER", 0.25],
    ["QUARTER", 0.25],
    ["DIME", 0.1],
    ["DIME", 0.1],
    ["PENNY", 0.01],
    ["PENNY", 0.01],
    ["PENNY", 0.01],
    ["PENNY", 0.01]
];
var obj = arr.reduce(function (out, el) {
    out[el[0]] = (out[el[0]] || 0) + el[1];
    return out;
}, {});
var out = Object.keys(obj).map(function (key) {
    return [key, obj[key]]
}); //[["TWENTY",80],["TEN",10],["FIVE",5],["ONE",1],["QUARTER",0.5],["DIME",0.2],["PENNY",0.04]]

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

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