array object merge javascript duplicate check code example

Example 1: javascript merge arrays of objects without duplicates

var merged = [...initialData, ...newData.filter(d => !ids.has(d.ID))];

Example 2: 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);