1

I have an array of arrays:

[
  [
    "Serta",
    "Black Friday"
  ],
  [
    "Serta",
    "Black Friday"
  ],
  [
    "Simmons",
    "Black Friday"
  ],
  [
    "Simmons",
    "Black Friday"
  ],
  [
    "Simmons",
    "Black Friday"
  ],
  [
    "Simmons",
    "Black Friday"
  ]
]

I need to get unique arrays only, like:

[
  [
    "Serta",
    "Black Friday"
  ],
  [
    "Simmons",
    "Black Friday"
  ] 
]

I know that for single array element I can use .filter() like:

array.filter((d,i)=>array.indexOf(d)==i)

but I'm not sure for this case, without any library like lodash.

yuriy636
  • 11,171
  • 5
  • 37
  • 42
Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85

1 Answers1

1

You could take a JSON string as key of the array in a hash table and filter with it.

var data = [["Serta", "Black Friday"], ["Serta", "Black Friday"], ["Simmons", "Black Friday"], ["Simmons", "Black Friday"], ["Simmons", "Black Friday"], ["Simmons", "Black Friday"]],
    hash = Object.create(null),
    result = data.filter(a => !hash[JSON.stringify(a)] && (hash[JSON.stringify(a)] = true));

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392