-1

I am stuck on this problem asking to remove duplicate values from an array. Here are the instructions:

Write a function called uniq that takes in an array and a callback function. Remove any duplicate values from the array, and invoke the callback with the modified array as an argument.

Rilo27
  • 26
  • 4
  • We'll be happy to help you with your problem, but: _"Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it."_ – Andreas Nov 29 '17 at 05:51
  • Hi @Brittany, I'm sorry but on Stackoverflow you are supposed to provide an example of what you have tried :-| –  Nov 29 '17 at 05:51
  • This is already answered here: https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array – Kin Nov 29 '17 at 05:53

1 Answers1

1
function myFunction(myArray, callBack){

 var unique = myArray.filter(function(item, pos) {
   //validates whether the first occurrence of current item in array
   // equals the current position of the item (only return those items) 
   return myArray.indexOf(item) == pos;
 });

 //wrap your result and pass to callBack function 
 callBack(unique);

}

Call your function using

myFunction([1,2,2,4,5], function(result){
console.log(result);
})

Or

function callBack(result){console.log(result);} 
myFunction([1,2,2,3,4], callBack);
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75