Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
JavaScript filter method not giving unique result

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 []

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

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

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

console.log(uniqueArray)
about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!