I’ve looked a pile of answers to related questions, but haven’t found anything that directly handles this. Should be simple, but I’m drawing a blank.
If I have an array like this:
const items = [
{id: uuid-uuu, itemId: 110, nbr: 135, recordID: 222},
{id: uuid-www, itemId: 111, nbr: 134, recordID: 555},
{id: uuid-xxx, itemId: 111, nbr: 134, recordID: 555},
{id: uuid-yyy, itemId: 111, nbr: 134, recordID: 111},
{id: uuid-zzz, itemId: 112, nbr: 135, recordID: 222},
]
And I want to look at itemId, nbr, and recordID values, (but NOT id and other keys not included in this example) and find any objects where those are all the same, and return those unchanged duplicated objects (multiple) in a new array.
So in this case, I just want this as the result:
const duplicateItems = [
{id: uuid-www, itemId: 111, nbr: 134, recordID: 555},
{id: uuid-xxx, itemId: 111, nbr: 134, recordID: 555},
]
I don’t want duplicates removed, I don’t want only unique items – I only want the duplicate list.
Should I be using reduce, filter, Map, etc.?
Needs to be in plain JavaScript, but can use latest / ES6 functions.
2
Answers
[NOTE: REQUIRES POLYFILLS]
Could try something like this;
https://jsfiddle.net/tk13nzhm/32/
So you need to generate a key based off the items you want. Easiest way is just using reduce. Reduce holds an object. You create an array with your key. You push in the objects into that array. After you are done with loop, filter your array for dupes and make it one array with flat.