0

So here is what I'm trying to do.

I have an array that contains more arrays of dates (which there are multiples of) and individual scores. It looks like this:

var example = [
    ["11/7/2015", 4],
    ["11/7/2015", 7],
    ["11/7/2015", 2],
    ["11/8/2015", 2],
    ["11/8/2015", 7],
    ["11/9/2015", 0],
    ["11/10/2015", 1]
];

My goal is to iterate through this entire array (it has around 900 cells), that can add/combine the scores of the dates that are similar, and overall removes all duplicate dates with the scores added together.

So the end result of the first array should look like this:

var example = [
    ["11/7/2015", 13],
    ["11/8/2015", 9],
    ["11/9/2015", 0],
    ["11/10/2015", 1]
];

As you can see, the duplicate dates were removed and the scores of each duplicate cell were added under one cell.

I tried doing this by using a for loop like this (using a duplicate of the array so I can use it as comparison to the original):

for(var i = 1; i < example.length; i--){
  if(example[i][0] === dummyArray[i-1][0]){
    example[i-1][1] += dummyArray[i][1];
    example.splice(i,1);
  } else{

  }
}

But I can't use i-1 syntax inside the loop and not sure where to go from here. My goal is to do this in pure javascript and not use any libraries.

3 Answers 3

3

Here's one way of doing that:

var dateScoreAggregateMap = {};

example.forEach(function(pair){
    if(dateScoreAggregateMap[pair[0]]){
        dateScoreAggregateMap[pair[0]] += pair[1];
    } else {
        dateScoreAggregateMap[pair[0]] = pair[1];
    }
});

example = Object.keys(dateScoreAggregateMap).map(function(date){
    return [date, dateScoreAggregateMap[key]];
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I'll try this out and let you know how it goes.
2

If you don't mind the result in object form:

var example = [
    ["11/7/2015", 4],
    ["11/7/2015", 7],
    ["11/7/2015", 2],
    ["11/8/2015", 2],
    ["11/8/2015", 7],
    ["11/9/2015", 0],
    ["11/10/2015", 1]
];

var out = example.reduce(function (p, c) {
    if (!p[c[0]]) p[c[0]] = 0; // key doesn't exist in object, add it and set it to zero
    p[c[0]] += c[1]; // add the score to the existing key
    return p;
}, {});

alert(JSON.stringify(out));

1 Comment

Thanks. I'll try this out and will respond accordingly. It's just a little weird reading if statements without the brackets. I'll get a hang of it though.
1

First generate results through an object:

var obj = {};

example.map(function(el) {
    el[0] in obj ? obj[el[0]] += el[1] : obj[el[0]] = el[1];
    return obj;
});

el is the array pair. You ask if the date el[0] is in the object. If so, add the corresponding value. If not, set the key with the corresponding value.

Translate the object to an array:

var arr = Object.keys(obj).map(function(el) {
    return [el, obj[el]];
});

or (what is equal):

var arr = [];
for(k in obj) {
    var pair = [k, obj[k]];
    arr.push(pair);
}

JSFiddle

2 Comments

This is no different from my own answer, which was more than 30 minutes earlier than yours. You could have improved upon mine possibly, instead of repeating the same thing.
Oh sorry, I didn't think about that. Thanks for the hint! BTW, I've upvoted your answer.

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.