showzyl/my-blog

【JS Trick】用`reduce`替换`filter`和`map`的使用

Opened this issue · 0 comments

let arr = [0, 1, 2, 3, 4]

let a = arr.filter(item => item > 1).map(item => item += 1)

let b = arr.reduce(function(list, items, index, array){
  if(items > 1){ 
    list.push((items + 1)) 
  }
  // console.log(`list: `, list)
  // console.log(`items: `, items)
  // console.log(`index: `, index)
  // console.log(`array: `, array)
  return list
}, []);

console.log(`a: `, a)
console.log(`b: `, b)