I have an array of objects with duplicates and I'm trying to get a unique listing, where uniqueness is defined by a subset of the properties of the object. For example,
Current JSON Object:
[{"x":6811,"y":15551,"a":"a"},
{"x":6811,"y":15551,"a":"b"},
{"x":6811,"y":15551,"a":"c"},
{"x":6811,"y":15552,"a":"c"},
{"x":6812,"y":15551,"a":"c"}]
How to group by two property
The last result is
[{"x":6811,"y":15551,"a":["a","b","c"]},
{"x":6811,"y":15552,"a":["c"]},
{"x":6812,"y":15551,"a":["c"]}]
How to use underscore to make it unique and generate a merge "a" Key
You can use
groupBy
to create a composite key onx
andy
. Then, usemap
to iterate through the grouped data.Here is a vanilla js solution using
reduce
andObject.values
Demo