1

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]
4
  • why do i need to use proper terminology if you already know what i mean lol. Commented Feb 6, 2014 at 16:28
  • @LargeTuna So you at least sound like you might know what you're talking about. That's more important than you seem to think. Commented Feb 6, 2014 at 16:30
  • It clearly is to some people :rolleyes: Commented Feb 6, 2014 at 16:32
  • 1
    I get it. Again why is it important to seem like I know what I am talking about, results are all I need. I clearly seemed like i knew what i was talking about when i posted the post. Commented Feb 6, 2014 at 16:32

2 Answers 2

2

This should do the trick:

var myArray = [
  [295518, 2],
  [123518, 123],
  [295518, 43],
  [295518, 65],
  [234518, 3],
  [567518, 56]
]

var result = {};

for (var 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);

Here's the same thing with jQuery :

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);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Here's a working example...

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming myArray always contains arrays with a length of 2, you could do the following:

var myArray = [
    [295518, 2],
    [123518, 123],
    [295518, 43],
    [295518, 65],
    [234518, 3],
    [567518, 56]
];
var allSums = {};

$.each(myArray, function( i, pair ) {
    if( allSums[pair[0]] ) {
        allSums[pair[0]] += pair[1];
    }
    else {
        allSums[pair[0]] = pair[1];
    }
});

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.