Search code examples
javascriptarraysnode.jsunique

Get all unique values in an array (remove duplicates) fot nest array / object


i know there has many answer for unique array

but they can't handle with array of array


what i want is

source array

[
    1,
    0,
    true,
    undefined,
    null,
    false,
    ['a', 'b', 'c'],
    ['a', 'b', 'c'],
    ['a', 'c', 'b'],
    { a: { b: 2 } },
    { a: { b: 2 } },
    { a: { b: 3 } },
    { a: { b: undefined } },
    { a: {  } },
    { a: { b: 3, c: undefined } },
]

the return

[
    1,
    0,
    true,
    undefined,
    null,
    false,
    ['a', 'b', 'c'],
    ['a', 'c', 'b'],
    { a: { b: 2 } },
    { a: { b: 3 } },
    { a: { b: undefined } },
    { a: {  } },
    { a: { b: 3, c: undefined } },
]
  • arr-unique can handle object[], but can't handle array of array
  • Set can't too

fail code

console.log(array_unique(data));

console.log([...new Set(data)]);

console.log(data.filter(function (el, index, arr)
{
    return index == arr.indexOf(el);
}));

===================

update

i create a module for this array-hyper-unique, but didn't use json stringify because it has a bug when valuse is regexp


Solution

  • One easy method would be to stringify the arrays and objects in order to identify duplicates:

    const input = [
        1,
        true,
        ['a', 'b', 'c'],
        ['a', 'b', 'c'],
        { a: { b: 2 } },
        { a: { b: 2 } },
        { a: { b: 3 } },
        { a: { b: 3, c: undefined } },
    ];
    
    const outputSet = new Set();
    const stringifiedObjs = new Set();
    input.forEach(item => {
      if (typeof item !== 'object') outputSet.add(item);
      else {
        // replace undefineds with something, else they'll be ignored by JSON.stringify:
        const stringified = JSON.stringify(
          item,
          (k, v) => v === undefined ? 'undefined-value' : v
        );
        if (!stringifiedObjs.has(stringified)) {
          outputSet.add(item);
          stringifiedObjs.add(stringified)
        }
      }
    });
    console.log([...outputSet]);