score:3

Accepted answer

You can use some maps and reduces to convert this in one line:

var quantities = [{"type": "apple","number": 51}, 
{"type": "orange","number": 19}, 
{"type": "lemon","number": 11}];

var joined = quantities.map(function(x) {
  return Array.apply(null, new Array(x.number)).map(function (y) {
    return x.type; }); }).reduce(function(prev, z) {
      return prev.concat(z); }, []) 

console.log(joined);

In case you were wondering what is Array.apply(null, new Array(x.number))… this is an idiom to create an initialized array from an uninitialized array of specified length, for the reason that the former is mappable and the latter is not.


Related Query

More Query from same tag