-1

i am trying to filter the unique ones from a given array using filter method but its throwing an empty array, here is the code

function dual(a) {
  if (Array.isArray(a)) {
    let unique = a.filter((val, index) => {
      a.indexOf(val) == index
    })
    return unique;
  }
}
console.log(dual([3, 1, 1, 2, 2])) //expected result [3,1,2] but showing []
Jamiec
  • 133,658
  • 13
  • 134
  • 193
Rediat
  • 11
  • 3

2 Answers2

2

You may use Set() to create a unique array:

const arr = [3,1,1,2,2]

var uniqueArray = [...new Set(arr)]

console.log(uniqueArray)
Rayl
  • 188
  • 1
  • 8
1

You are missing return statements from both your function and your filter array method.

Jack
  • 220
  • 1
  • 8