admin管理员组文章数量:1277397
So I have a multidimensional array like:
myArr = [["venue",2],["venue",16],["inning",2],["inning",4],["inning",32],["hithard", 4]]
I would like to add the similar values up. So in the end I just have:
"venue" = 18, "inning" = 38, and "hithard" = 4.
Can you give me an example of how to acplish this? Either with Javascript and/or jQuery
Thanks!
So I have a multidimensional array like:
myArr = [["venue",2],["venue",16],["inning",2],["inning",4],["inning",32],["hithard", 4]]
I would like to add the similar values up. So in the end I just have:
"venue" = 18, "inning" = 38, and "hithard" = 4.
Can you give me an example of how to acplish this? Either with Javascript and/or jQuery
Thanks!
Share Improve this question edited May 8, 2012 at 3:51 Ram 145k16 gold badges172 silver badges200 bronze badges asked May 7, 2012 at 19:33 ksumarineksumarine 7822 gold badges13 silver badges33 bronze badges 7-
Do you want an array or object? Object is
{ venue=18, inning=36, hithard=4}
and Array is[["venue", 18], ["inning", 38], ["hithard", 4]]
– Selvakumar Arumugam Commented May 7, 2012 at 19:52 - Well I'm going to be turning this into a querystring to pass to a service so I'm sure either will work well – ksumarine Commented May 7, 2012 at 19:54
- you need a hash (object) – Gavriel Commented May 7, 2012 at 19:55
- 1 I'm going to add this to my list of interview questions, it's brought out a ton of interesting approaches – Mike Robinson Commented May 7, 2012 at 20:00
- 1 @Corey you can always use $.param to parse the obj to a query string. – Selvakumar Arumugam Commented May 7, 2012 at 20:11
4 Answers
Reset to default 3I am not sure if you want an array or object. If object, stop it is 1st pass and tmp
in below code should return you the object as Object { venue=18, inning=38, hithard=4}
.
DEMO
var tmp = {}, keys;
for (var i = 0; i < myArr.length; i++) {
keys = myArr[i][0];
tmp[keys] = (tmp.hasOwnProperty(keys))?
(tmp[keys] + myArr[i][1]):myArr[i][1];
} //tmp - will return you a Object { venue=18, inning=38, hithard=4}
var output = [];
for (keys in tmp) {
output.push([keys, tmp[keys]]);
} //output will return you an array as [["venue", 18],["inning", 38],["hithard", 4]]
myArr = [["venue",2],["venue",16],["inning",2],["inning",4],["inning",32],["hithard", 4]];
values = {};
for (i=0;i<myArr.length;i++){
if ("undefined" == typeof values[myArr[i][0]]) {values[myArr[i][0]] = 0;}
values[myArr[i][0]] += myArr[i][1];
}
arr = [];
query_string = "";
for (i in values) {
// if you want it in an array:
arr.push('"' + i + '" = ' + values[i]);
query_string += (query_string.length ? "&" : "") + i + "=" + values[i];
}
console.log(arr);
DEMO: http://jsfiddle/Ta97E/2/
you can use values to create the query string
Check this code:
var final = {};
for (var i in myArr) {
var item = myArr[i];
final[item[0]] = (final[item[0]] || 0) + item[1];
}
console.log(final);
DEMO: http://jsfiddle/UVJEb/
Underscore solution:
sums = _.reduce(myArr, function (obj, item) {
obj[item[0]] = (obj[item[0]] || 0) + item[1];
return obj;
}, {});
// sums = {"venue":18,"inning":38,"hithard":4}
A little dirtier in jQuery
sums = {}
$.each(myArr, function (i, value) {
sums[value[0]] = (sums[value[0]] || 0) + value[1];
});
Edit: add jQuery version
本文标签: jqueryJavascript Multidimensional Array Add ValuesStack Overflow
版权声明:本文标题:jquery - Javascript Multidimensional Array: Add Values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741291924a2370599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论