-1

Now, in my object collection,obj:

{date:2015/9/11,name:Ann,apple:1},
{date:2015/9/12,name:Ann,apple:0},
{date:2015/9/13,name:Ann,apple:1},
{date:2015/9/11,name:Bob,apple:1},.....

and I print it out like this:

2015/9/11  Ann     1  
2015/9/12  Ann     0  
2015/9/13  Ann     1  
2015/9/11  Bob     1  
2015/9/12  Bob     1  
2015/9/13  Bob     1  

Here is my code

var index, objLen
for (index = 0, objLen = obj.length; index<objLen; ++index){
    console.log(obj[index].date +'\t'+ obj[index].name +'\t'+ result[index].apple)
}

I wish to output:

2015/9/13  Ann     2  
2015/9/13  Bob     3 

This shows how many apples Ann and Bob have eaten in this three days

I want to know how to get the output.

Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48
walterudoing
  • 115
  • 1
  • 2
  • 10
  • @AlokSwain I have tried a few times, and those do not work. This is simplified from a way more complicated project. – walterudoing Sep 11 '15 at 13:52

2 Answers2

1

You can use a Array.prorotype.reduce to merge the items with the same name, storing the results in an object.

var obj = [
    { date: '2015/9/11', name: 'Ann', apple: 1 },
    { date: '2015/9/12', name: 'Ann', apple: 0 },
    { date: '2015/9/13', name: 'Ann', apple: 1 },
    { date: '2015/9/11', name: 'Bob', apple: 1 },
    { date: '2015/9/12', name: 'Bob', apple: 2 },
    { date: '2015/9/13', name: 'Bob', apple: 1 }
];

var merged = obj.reduce(function(merged, item) {
    if (!merged.hasOwnProperty(item.name)) {
        merged[item.name] = { date: item.date, name: item.name, apple: 0 };
    }
    merged[item.name].date = item.date;
    merged[item.name].apple += item.apple;
    return merged;
}, {});
// merged == {
//     Ann: { date: '2015/9/13', name: 'Ann', apple: 2 },
//     Bob: { date: '2015/9/13', name: 'Bob', apple: 4 }
// }

But this one results into an object with the person's name as the key. If you want to convert this back to an array of merged items, you can can make an array out of the values of the merged object using a combination of Object.keys and Array.prototype.map:

merged = Object.keys(merged).map(function(item) {
    return merged[item];
});
// merged == [
//     { date: '2015/9/13', name: 'Ann', apple: 2 },
//     { date: '2015/9/13', name: 'Bob', apple: 4 }
// ]

You can then now use your code to print out the elements of this array.

Refer to the following links to read more about the different methods used in this solution:

Arnelle Balane
  • 5,437
  • 1
  • 26
  • 32
0

Some functional approach to group and count element by passed key:

var obj = [{
  date: '2015/9/11',
  name: 'Ann',
  apple: 1
}, {
  date: '2015/9/12',
  name: 'Ann',
  apple: 0
}, {
  date: '2015/9/13',
  name: 'Ann',
  apple: 1
}, {
  date: '2015/9/11',
  name: 'Bob',
  apple: 1
}, {
  date: '2015/9/12',
  name: 'Bob',
  apple: 2
}, {
  date: '2015/9/13',
  name: 'Bob',
  apple: 1
}];

function groupBy(key, collenction) {
  return collenction.reduce(function(group, element) {
    if (!group[element[key]]) {
      group[element[key]] = [element]
    } else {
      group[element[key]].push(element)
    }
    return group
  }, {})
}

function countCollectionBy(key, collenction) {
  return Object.keys(collenction).reduce(function(memo, element) {
    memo[element] = countBy(key, collenction[element])
    return memo;
  }, {})
}

function countBy(key, collenction) {
  return collenction.reduce(function(memo, element) {
    return memo + element[key]
  }, 0)
}

alert(JSON.stringify(countCollectionBy('apple', groupBy('name', obj))))
Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47