How do I select the unique items in this array. I need to compare both fields (a, b only, excluding c) and the examples I have seen show a map which takes a single field only.
You can use a nested filter, and check that the number of matching elements with the same a and b is 1:
const input = [{a:1,b:2,c:4}, {a:3,b:4,c:7}, {a:7,b:10,c:10}, {a:1, b:2,c:4}];
const output = input.filter(
({ a, b }) => input.filter(obj => obj.a === a && obj.b === b).length === 1
);
console.log(output);
For O(N) complexity instead, reduce into an object indexed by num_num, or something like that, and then select only the values which have one item in them:
You could reduce the array with a Map as an accumulator. Use a combination of a and b properties as key so that duplicates are removed. Then use Map#values to get the output
Thanks for all your replies, I think I forgot to mention, I also need the a single item of the duplicated item, not to completely ignore them. The sample code give above removes the duplicates completely, I need one instance of it.
@tmpdev It doesn't remove the duplicates completely. It keeps one item for every unique a and b. The output has 3 items. Only one item with a:1,b:2 is in the output
{a:3,b:4,c:7}, {a:7,b:10,c:10}?