0

say I have an array with the following ten values: 329,374,353,336,137,340,374,374,356,329 I want to create a new array which uses those values and creates a sum according to their position, so that the new array (based on those values) would look like so:

329, 329+374, 329+374+353, 329+374+353+336, ... using javascript alone (no jquery) how can I achieve that?

4
  • Btw: there's absolutely nothing that jQuery can do and plain JS can't. Commented May 12, 2015 at 23:16
  • @Shomz - the OP is just requesting a plain Javascript answer (with no jQuery). That's a very reasonable clarification of what is wanted. Commented May 12, 2015 at 23:17
  • @jfriend00, haha, had one ("jQ solves everything") about two minutes ago, so I know exactly what you mean. :) I just want the OP (and everyone else reading the comment) to be aware of that. Commented May 12, 2015 at 23:19
  • 1
    @Shomz I'm aware of that, that's why I never bothered learning jQ and have no intentions to do it in the near future. I just mentioned it, as jfriend00 said, so that I won't get answers in jQ... Commented May 12, 2015 at 23:28

2 Answers 2

4

You can use .map() and a running total variable:

var array = [329,374,353,336,137,340,374,374,356,329], sum = 0;
var result = array.map(function(item) {
    sum += item;
    return sum;
});
console.log(result);

Working demo: http://jsfiddle.net/jfriend00/9ekkk1r3/


var array = [329,374,353,336,137,340,374,374,356,329], sum = 0;
var result = array.map(function(item) {
    sum += item;
    return sum;
});

// show results in snippet
document.write("[" + result.join(',') + "]");

.map() is useful here because it both iterates through the source array and creates a new result array which is exactly what you want to do. Then, all your have to do to simplify the result is to keep a running total of the sum so far so you know what to add for each new value.

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

1 Comment

@Shomz - I modified the snippet to output to the window directly rather than in an alert. I'm not yet a fan of snippets myself, but I don't mind you put one in.
1
input = [329,374,353,336,137,340,374,374,356,329]
output = [input[0]]
for (var i = 1; i < input.length; ++i){
    output[i] = output[i-1]+input[i];
}

4 Comments

you know, it's a funny thing, just as I submitted the question I suddenly had the idea of doing exactly what you suggested here. I went over to my testing site and it worked. then when I returned here and there were already 2 answers. I guess sleeping only 3 hours last night has taken its toll :)
@user3086182 - just curious why do you prefer a manual for loop over .map() which is built exactly for an iteration which creates a new array.
@jfriend00 TBH I don't think I fully understand what .map() does and I don't have time to delve into that at moment, so I go with the simpler solution which gets the job done...
@user3086182 - when you get some time, you should learn the built-in array functions like .map(), .forEach() and .reduce() as they can be very useful.

Your Answer

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