Object array add values

Can someone help me with this javascript?
It's supposed to remove duplicates from an object array and add the values left.

http://jsfiddle.net/9vyuY/1/
LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?
 
zappafan2k2Commented:
The function needs to be defined before you try to use it.
http://jsfiddle.net/9vyuY/2/
All I did was move lines 3-5 down below the function definition.

Is that what you are looking for?
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
I feel stupid...Thanks!
0
 
RobOwner (Aidellio)Commented:
For future reference, this is the working code

var foo = [ { "a" : 15 }, { "b" : 25 }, { "a" : 15 }, {"c" : 13} ];


var uniqBy = function(ary, key) {
    var seen = {};
    return ary.filter(function(elem) {
        var k = key(elem);
        return (seen[k] === 1) ? 0 : seen[k] = 1;
    })
}

uniqs = uniqBy(foo, JSON.stringify);
values = uniqs.map(function(x) { for(var k in x) return x[k] });
sum = values.reduce(function(a, b) { return a + b });


alert(sum);

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.