Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get unique values from array of arrays [duplicate]

i have the following array

let arr =   [
  [
    "[email protected]",
    "[email protected]"
  ],
  [
    "[email protected]",
    "[email protected]"
  ]
]

i want get the unique values from this array. so im expecting my result to be like this

[
  [
    "[email protected]",
    "[email protected]",
    "[email protected]"
  ]  
]

i have used array unique function but not able to get the result

var new_array = arr[0].concat(arr[1]);
var uniques = new_array.unique();

this worked if i have two index but how about for multiple index?

like image 623
kanishka kumarasiri Avatar asked Jul 14 '19 11:07

kanishka kumarasiri


1 Answers

You can use .flat() to flatten your array and then use Set to get its unique values.

Demo:

let arr =   [
  [
    "[email protected]",
    "[email protected]"
  ],
  [
    "[email protected]",
    "[email protected]"
  ]
]

let arr2 = [...new Set(arr.flat(1))];
console.log(arr2)
like image 76
Cuong Le Ngoc Avatar answered Oct 22 '22 15:10

Cuong Le Ngoc