2017-11-29 2 views
0

配列から重複した値を削除するかどうかを尋ねるこの問題が発生しました。命令は次のとおりです。コールバックを使用して配列から重複した項目を削除する

配列とコールバック関数をとるuniqという関数を記述します。 配列から重複した値を削除し、変更された配列を引数としてコールバックを呼び出します。

+0

を使用して関数を呼び出し、これはあなたの学校の宿題ですか? – Eddie

+0

私たちはあなたの問題を手伝ってくれることを喜んでお勧めします:_ "宿題の助けを求める質問は、問題を解決するために今までに行った作業の概要と解決している問題の説明を含める必要がありますそれは。 "_ – Andreas

+0

こんにちは@ブリッタニー、私は申し訳ありませんが、Stackoverflowであなたが試したものの例を提供することになっています: - | – leaf

答えて

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); 

} 

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

それとも

function callBack(result){console.log(result);} 
myFunction([1,2,2,3,4], callBack); 
関連する問題