showing results for - "merger douplicate array object"
Lya
02 Jan 2020
1var 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" } ],
2    result = Object.values(myArr.reduce((r,o) => {
3      r[o.Company] = r[o.Company] || {'Company': o.Company, 'Models' : []};
4      r[o.Company]['Models'].push({[o.model]:{'screen_size' : o.screen_size}});
5      return r;
6    }, {}));
7
8console.log(result);