Javascript - Reduce array with multiple-key object

2019-09-13 01:43发布

问题:

This question already has an answer here:

  • Group array items using object 13 answers

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:

[
    {
        "language" : "english",
        "type" : "a",
        "value" : "value1"
    },
    {
        "language" : "english",
        "type" : "a",
        "value" : "value2"
    },
    {
        "language" : "english",
        "type" : "b",
        "value" : "value3"
    },  
    {
        "language" : "spanish",
        "type" : "a",
        "value" : "valor1"
    }
]

into this:

[
    {
        "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"
    }
]

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

P.S EcmaScript6 and additional JS library suggestions are also welcome.

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 && 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));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

回答1:

This is what you need?

var baseData= [
    {
        "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 newData = [];
baseData.forEach(function(item, index) {
  if (newData.length === 0) {
    newData.push(item);
    
  } else {
    var dIndex = -1;
    newData.forEach(function(itm, idx) {
      if (item.language === itm.language && item.type === itm.type) dIndex = idx;
    });
    
    if (dIndex !== -1) {
      var oldValue = newData[dIndex].value;
      
      if (typeof(oldValue).toString() === 'string') {
        newData[dIndex].value = [oldValue, item.value];
      }
    } else {
      newData.push(item);
    }
  }
});
console.log(newData);