Javascript基本算法演练 Seek and Destroy
转载自:http://aeroj-blog.logdown.com/posts/435808
Seek and Destroy
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
想法:
1.用 Arguments object 將所有參數包裝成一個陣列 arg
2.用Array.splice()將arr參數移除
3.用Array.indexOf()將arr除外的arg陣列做為過濾的條件
4.將arr中符合filter條件的字元刪除
5.回傳最後的arr
代码如下:
function destroyer(arr) { var args = Array.prototype.slice.call(arguments); args.splice(0,1); return arr.filter(function(ele){ return args.indexOf(ele) === -1; }); } destroyer([1, 2, 3, 1, 2, 3], 2, 3);