0

For example: I have array that looks like this:

let arr = [{
    name: "Peter"
}, {
    name: "Peter"
}, {
    name: "John"
}, {
    name: "Peter"
}, {
    name: "Sarah"
}, {
    name: "John"
}]

I need to create array, that will be look like this:

let dupArray = [
    [{
        name: "Peter"
    }, {
        name: "Peter"
    }, {
        name: "Peter"
    }],
    [{
        name: "John"
    }, {
        name: "John"
    }],
    [{
        name: "Sarah"
    }]
]

Again, it's not about duplicating, it's about creating new array with duplicates grouped.

jusinejo
  • 3
  • 2
  • So to be explicit, you want to create a 2nd array named 'dupArray' that contains the duplicate names grouped together? Not a count, just grouped? With this exact data? What code have you got so far? – Liam MacDonald Aug 15 '17 at 10:52
  • 1
    @sascha10000 this isn't about duplicating an array really, it's about creating groups _of_ duplicates in an array – George Aug 15 '17 at 10:52
  • Try `dupArray = arr.sort();` it sorts it alphabetically which in turn will group the duplicates together. – Deckerz Aug 15 '17 at 10:52
  • @jusinejo can you show us what you have tried so far? – Mμ. Aug 15 '17 at 10:56
  • 2
    Possible duplicate of [What is the most efficient method to groupby on a javascript array of objects?](https://stackoverflow.com/questions/14446511/what-is-the-most-efficient-method-to-groupby-on-a-javascript-array-of-objects) – George Aug 15 '17 at 10:56

2 Answers2

3

Reduce them to a Map and get the values():

let arr = [{
  name: "Peter"
}, {
  name: "Peter"
}, {
  name: "John"
}, {
  name: "Peter"
}, {
  name: "Sarah"
}, {
  name: "John"
}];

let dupArr = 
    [...arr.reduce((a,b) => 
        a.set(b.name , (a.get(b.name) || []).concat(b)),
     new Map()).values()];

console.log(dupArr);
baao
  • 71,625
  • 17
  • 143
  • 203
  • Thank's, that's what i need. I new to JS, what this three dot's `...arr` means? – jusinejo Aug 15 '17 at 11:55
  • @jusinejo That [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator), it converts an iterable to an array – baao Aug 15 '17 at 12:09
0

var arr = [{name: "Peter"},{name: "Peter"},{name: "John"},{name: "Peter"},{name: "Sarah"},{name: "John"}]

  var group = function(ar){
      // counts the element per group
      var swap = {};
      for(a of ar){
          if(swap[a.name] !== undefined){
              swap[a.name] = swap[a.name] + 1;
          }
          else {
              swap[a.name] = 1;
          }
      }

      // here you could sort alphabetically

      // creates your wanted format
      var target = [];
      for(s in swap){
          var len = swap[s];
          var subArray = [];
          for(var i = 0; i < len; i++){
              subArray.push({ "name" : s });
          }
          target.push(subArray);
      }

      return target;
  }

  document.getElementById("content").innerHTML = JSON.stringify(group(arr));
<pre id="content"></pre>

In the first part of the group function the whole thing will be grouped by counting. And afterwards your wished representation will be generated. By just adding {"name" : } to an array according to the counts inside the swap array (the naming is arbitrary).

sascha10000
  • 1,245
  • 1
  • 10
  • 22