1

I have an object that has an unknown number of properties, the values of which are non-empty arrays e.g.

const obj = {
  c1: ['a', 'b', 'c'],
  c2: ['a', 'c', 'b'],
  cx: ['a', 'b'],
  c3: ['d'],
  c4: ['d'],
  c5: ['e'],
  ...
}

I'm looking to find any duplicate properties, where the order of the array items isn't considered, and the result would let me know which properties match e.g. [c1,c2], [c3, c4]

I can probably use a combination of Object.entries, nested forEach, sort the arrays before comparing.... but was thinking there may be some more concise approach with lodash that I haven't considered

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Drenai
  • 11,315
  • 9
  • 48
  • 82
  • 1
    What do you consider to be "duplicate"? Does it mean *full overlap* or just partial? E.g., if you had a `d: ["a", "b"]` would that be considered duplicate of `c1` and `c2`? – VLAZ Nov 11 '21 at 12:42
  • I updated the question to cover this - but no, in that case `d` wouldn't be considered a duplicate of `c1` or `c2` – Drenai Nov 11 '21 at 12:45
  • Without lodash: https://stackoverflow.com/a/6230314/12057512. With lodash: https://stackoverflow.com/a/27817275/12057512 – Emil Karlsson Nov 11 '21 at 12:46
  • 1
    Thank you. I wanted to make sure I understood the question correctly. I also updated your title to represent the problem better, hope it is alright. – VLAZ Nov 11 '21 at 12:46
  • 1
    @EmilKarlsson note that the Lodash answer is not actually completely correct - checking with `["a", "b", "b", "c"]` and `["a", "c", "b"]` would report them as duplicate but the two arrays aren't because they have different number of `"b"`s. Same if the second one was `["a", "c", "c", "b"]` in that case you'd also get an empty result from `xor` but the two arrays aren't composed of the same elements. – VLAZ Nov 11 '21 at 13:10

0 Answers0