总结彩民在JavaScript阵列 [英] Summing Totals In JavaScript Array

查看:136
本文介绍了总结彩民在JavaScript阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被我的页面上创建一个动态的JavaScript数组(没有这些数字将永远是相同的,许多变化),例如:

  myArray的[1] = [295518 2]。
 myArray的[2] = [123518,123];
 myArray的[3] = [295518 43];
 myArray的[4] = [295518 65];
 myArray的[5] = [234518,3];
 myArray的[6] = [567518 56];

此阵的说明:

  [ID号,数量]
 myArray的[1] = [295518,2];

我需要组中的所有类ID数组元素在一起,总结他们的价值观。

 例如:295518将增加2 + 43 + 65 = 110

然后,我需要它来更新值的页面上的ID:

  $(#295518)文本(110)。

所以,我在想什么,这是的,但我不知道如何将它使用JavaScript编写:

  [开始JavaScript的循环]
      [抢不同的IDS]
           [ID开始循环]
                [抢基于ID的所有值]
                [总结他们的数量值]
                [网页上的总和值设置id元素]
                     [例如:$(#295518)文本(110); ]
           [结束ID循环]
 [/结束的JavaScript循环]


解决方案

这应该做的伎俩...

  myarray的无功= [];
myArray.push([295518,2]);
myArray.push([123518,123]);
myArray.push([295518 43]);
myArray.push([295518 65]);
myArray.push([234518,3]);
myArray.push([567518 56]);VAR的结果= {};$(myarray的)。每个(函数(){
    VAR键=此[0];
    VAR值=本[1];
    如果(结果[关键]){
        结果[键] + =价值;
    }其他{
        结果[关键] =价值;
    }
});的console.log(结果);

这里的工作示例...

和只是为了完整,这里是同样的事情没有的jQuery(万一你需要它没有)......

  myarray的无功= [];
myArray.push([295518,2]);
myArray.push([123518,123]);
myArray.push([295518 43]);
myArray.push([295518 65]);
myArray.push([234518,3]);
myArray.push([567518 56]);VAR的结果= {};为(i的myArray的){
    VAR键= myArray的[I] [0];
    VAR值= myArray的[I] [1];
    如果(结果[关键]){
        结果[键] + =价值;
    }其他{
        结果[关键] =价值;
    }
}的console.log(结果);

I have a dynamic JavaScript array that is being created on my page (none of these numbers will ever be the same, many variations), for example:

 myArray[ 1 ] = [295518, 2];
 myArray[ 2 ] = [123518, 123];
 myArray[ 3 ] = [295518, 43];
 myArray[ 4 ] = [295518, 65];
 myArray[ 5 ] = [234518, 3];
 myArray[ 6 ] = [567518, 56];

Explanation of this array:

                [id number, quantity]
 myArray[ 1 ] = [295518,    2];

I need to group all of the like id array elements together and sum their values.

 For instance: 295518 would add 2 + 43 + 65 = 110

Then I need it to update the id on the page with the value:

 $("#295518").text(110); 

So what I am thinking is this, but I am not sure how to write it in JavaScript:

 [start JavaScript loop]
      [grab distinct ids]
           [start id loop]
                [grab all values based on id]
                [sum their qty values]
                [set the id element on the page with the sum value]
                     [example: $("#295518").text(110); ]
           [end id loop]
 [/end JavaScript loop]

解决方案

This should do the trick...

var myArray = [];
myArray.push([295518, 2]);
myArray.push([123518, 123]);
myArray.push([295518, 43]);
myArray.push([295518, 65]);
myArray.push([234518, 3]);
myArray.push([567518, 56]);

var result = {};

$(myArray).each(function() {
    var key = this[0];
    var value = this[1];
    if (result[key]) {
        result[key] += value;
    } else {
        result[key] = value;
    }
});

console.log(result);

Here's a working example...

And just for completeness, here's the same thing without jQuery (in case you ever need it without)...

var myArray = [];
myArray.push([295518, 2]);
myArray.push([123518, 123]);
myArray.push([295518, 43]);
myArray.push([295518, 65]);
myArray.push([234518, 3]);
myArray.push([567518, 56]);

var result = {};

for (i in myArray) {
    var key = myArray[i][0];
    var value = myArray[i][1];
    if (result[key]) {
        result[key] += value;
    } else {
        result[key] = value;
    }
}

console.log(result);

这篇关于总结彩民在JavaScript阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆