Merger Douplicate array Object code example

Example: Merger Douplicate array Object

var myArr=[{ "Company": "Samsung", "model": "Galaxy S2", "screen_size": "5.5" }, { "Company": "Samsung", "model": "Galaxy S3", "screen_size": "5.5" }, { "Company": "Samsung", "model": "Galaxy S4", "screen_size": "5.5" }, { "Company": "Xiaomi", "model":"Redmi 2", "screen_size": "4.7" }, { "Company": "Xiaomi", "model": "Redmi 4", "screen_size": "5" }, { "Company": "Xiaomi", "model": "Redmi Note 4", "screen_size": "5.5" } ],
    result = Object.values(myArr.reduce((r,o) => {
      r[o.Company] = r[o.Company] || {'Company': o.Company, 'Models' : []};
      r[o.Company]['Models'].push({[o.model]:{'screen_size' : o.screen_size}});
      return r;
    }, {}));

console.log(result);

Tags:

Misc Example