How to extract different values from multiple objects?

I have an array containing objects with the same properties, but different keys. I need to retrieve those objects that have different revitid.
The array has the following contents

0: {revitid: "281814", viewid: 3298, level: "", building: "", room: "", ...}
1: {revitid: "281857", viewid: 3299, level: "", building: "", room: "", ...}
2: {revitid: "281899", viewid: 3300, level: "", building: "", room: "", ...}
3: {revitid: "281957", viewid: 3301, level: "", building: "", room: "", ...}
4: {revitid: "281814", viewid: 3298, level: "", building: "", room: "", ...}
5: {revitid: "281857", viewid: 3299, level: "", building: "", room: "", ...}
6: {revitid: "281957", viewid: 3302, level: "", building: "", room: "", ...}
7: {revitid: "282264", viewid: 3304, level: "", building: "", room: "", ...}


I need to extract from this array of objects whose revitid meet once, i.e. the object No. 2, 4, 7.
March 19th 20 at 08:49
2 answers
March 19th 20 at 08:51
Solution
const
 count = data.reduce((acc, n) => (acc[n.revitid] = (acc[n.revitid] || 0) + 1, acc), {}),
 unique = Object.entries(count).filter(n => n[1] === 1).map(n => n[0]),
 newData = data.filter(n => unique.includes(n.revitid));

or

const newData = data.filter((n, i, arr) => arr.filter(m => m.revitid === n.revitid).length === 1);

or

const newData = Object.values(data.reduce((acc, n) => (acc[n.revitid] = acc[n.revitid] === undefined ? n : false, acc), {})).filter(Boolean);
Thank you very much, your help is invaluable. - Zora_Beer commented on March 19th 20 at 08:54
March 19th 20 at 08:53
Solution
Another option:
const notUniqueRevitids = data
 .reduce((acc, obj) => {
 if (acc[0].size === acc[0].add(obj.revitid).size) {
acc[1].add(obj.revitid);
}
 return acc;
 }, [new Set(), new Set()])[1];

const uniqueObj = data.filter(obj => !notUniqueRevitids.has(obj.revitid));
Thank you very much. - Zora_Beer commented on March 19th 20 at 08:56

Find more questions by tags JavaScript