Problem: 2634. 过滤数组中的元素 上
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
var filter = function(arr, fn) { const array=[] arr.forEach((value,index)=>{ if(fn(value,index)){ array.push(value) } }) return array };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
var filter = function(arr, fn) { const array = []
for(let i=0;i<arr.length;i++){ if(fn(arr[i],i)){ array.push(arr[i]) } }
return array }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
var filter = function(arr, fn) { var array = []
array = arr.reduce((reduceArr, value, index) => { if (fn(value, index)) reduceArr.push(value) return reduceArr }, [])
return array }
|
1 2 3 4 5 6 7 8 9 10 11
|
var filter = function(arr, fn) { var array = [] array = arr.filter(fn)
return array }
|