Javascript 使用回调从数组中删除重复项
我被这个问题困住了,要求从数组中删除重复的值。以下是说明: 编写一个名为uniq的函数,该函数接受一个数组和一个回调函数。 从数组中删除任何重复的值,并将修改后的数组作为参数调用回调Javascript 使用回调从数组中删除重复项,javascript,arrays,callback,Javascript,Arrays,Callback,我被这个问题困住了,要求从数组中删除重复的值。以下是说明: 编写一个名为uniq的函数,该函数接受一个数组和一个回调函数。 从数组中删除任何重复的值,并将修改后的数组作为参数调用回调 function myFunction(myArray, callBack){ var unique = myArray.filter(function(item, pos) { //validates whether the first occurrence of current item in arr
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);
}
使用
myFunction([1,2,2,4,5], function(result){
console.log(result);
})
或
我们很乐意帮助您解决问题,但是:“要求家庭作业帮助的问题必须包括您迄今为止为解决问题所做工作的总结,以及您解决问题的困难描述。”嗨@Brittany,很抱歉,关于Stackoverflow,您应该提供一个您尝试过的示例:-这里已经回答了这个问题:
function callBack(result){console.log(result);}
myFunction([1,2,2,3,4], callBack);