Javascript - Reduce array with multiple-key objects
<p>I want to modify a Javascript array, so that the elements having the same values for specifies properties merge into one object in a way that the other properties are kept as a comma-separated string, JSON string, or an array. Basically, I want to turn this:</p>

<pre><code>[
    {
        "language" : "english",
        "type" : "a",
        "value" : "value1"
    },
    {
        "language" : "english",
        "type" : "a",
        "value" : "value2"
    },
    {
        "language" : "english",
        "type" : "b",
        "value" : "value3"
    },  
    {
        "language" : "spanish",
        "type" : "a",
        "value" : "valor1"
    }
]
</code></pre>

<p>into this:</p>

<pre><code>[
    {
        "language" : "english",
        "type" : "a",
        "value" : ["value1" , "value2"]  // A Json string is welcome too
    },
    {
        "language" : "english",
        "type" : "b",
        "value" : "value3"
    },  
    {
        "language" : "spanish",
        "type" : "a",
        "value" : "valor1"
    }
]
</code></pre>

<p>I have tried iterating and filtering, then upserted the object as given in the snippet. But I wonder if there is a <strong>more elegant way</strong> to do that.</p>

<p>P.S EcmaScript6 and additional JS library suggestions are also welcome.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var originalArray = [
 {
  "language" : "english",
  "type" : "a",
  "value" : "value1"
 },
 {
  "language" : "english",
  "type" : "a",
  "value" : "value2"
 },
 {
  "language" : "english",
  "type" : "b",
  "value" : "value3"
 }, 
 {
  "language" : "spanish",
  "type" : "a",
  "value" : "valor1"
 }
];

var resultingArray = [];

// iterate through the original array
$.each(originalArray, function(i, val) {
  // apply filter on key properties (i.e. language and type)
  var result = resultingArray.filter(function( obj ) {
    return (obj.language === val.language &amp;&amp; obj.type === val.type);
  });
  // if a record exists, update its value
  if (result.length === 1) {  
    result[0].value += (", " + val.value);
  } 
  // else, add value
  else if (result.length === 0) {
    resultingArray.push(val);
  }
  // if multiple rows exist with same key property values...
  else {
    alert("Too many records with language '" + val.language + "' and type '" + val.type + "'");
  }
});

console.log(JSON.stringify(resultingArray));</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt;</code></pre>
</div>
</div>
</p>