-4
var inputArr = [{
            name: "Smith",
            points: 50
        },
        {
            name: "Rock",
            points: 27
        },
        {
            name: "John",
            points: 50
        },
        {
            name: "Rock",
            points: 800
        },
        {
            name: "Smith",
            points: 20
        },
        {
            name: "John",
            points: 80
        }]

outputArr = {
    "Smith" : 70,
    "JOhn" : 130,
    "Rock" : 827
} 

name should be unique, and unique name points should be consolidated. Ex : We have 2 number Smith with different points with 50 and 20. so result should be {"smith" : 70}

chandu
  • 21
  • 3
  • given arrray is inputArr, result should in the form of outputArr. – chandu Sep 26 '19 at 08:49
  • please add your try. – Nina Scholz Sep 26 '19 at 08:49
  • Please format your code and provide a description. – Slawomir Wozniak Sep 26 '19 at 08:50
  • 1
    Possible duplicate of [Most efficient method to groupby on an array of objects](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects) – Cid Sep 26 '19 at 08:51
  • Given array is inputArr, and result should be in outputArr form – chandu Sep 26 '19 at 08:52
  • 1
    Firstly, welcome to stackoverflow. Unfortunately you've mistaken this site as a code writing service... instead, we expect you to show what you've already tried to solve the issue. Please have a read of the [help] and the [ask] section in particular – freefaller Sep 26 '19 at 08:55

3 Answers3

0
    var inputArr = [{ name: "Smith", points: 50 }, { name: "Rock", points: 27 }, { name: "John", points: 50 }, { name: "Rock", points: 800 }, { name: "Smith", points: 20 }, { name: "John", points: 80 }]
       outputArr ={};
        for(x of inputArr ){
            var total=0;
            for(y of inputArr){
                if(x.name==y.name){
                    total+=y.points;
                }
            }
            if(!outputArr[x.name]){
                outputArr[x.name]=total;
            }
        }
console.log(outputArr);
0

You could take an object and reduce the array by taking name and points and assign the added points to the property with name.

var input = [{ name: "Smith", points: 50 }, { name: "Rock", points: 27 }, { name: "John", points: 50 }, { name: "Rock", points: 800 }, { name: "Smith", points: 20 }, { name: "John", points: 80 }],
    result = input.reduce((r, { name, points }) => {
        r[name] = (r[name] || 0) + points;
        return r;
    }, {});

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

I think that is the solution to the problem

var inputArr = [{ name: "Smith", points: 50 }, { name: "Rock", points: 27 }, { name: "John", points: 50 }, { name: "Rock", points: 800 }, { name: "Smith", points: 20 }, { name: "John", points: 80 }]
var outputArr = {};
 inputArr.forEach(myFunction)

function myFunction(item, index, arr) {
 if(typeof outputArr[item.name] === "undefined"){
  outputArr[item.name] = parseInt(item.points);
 }else{
  outputArr[item.name] = parseInt(outputArr[item.name] + item.points);
 }
}
console.log(outputArr);
Ivan Karaivanov
  • 343
  • 1
  • 9