发布时间:2023-05-03 文章分类:WEB开发, 电脑百科 投稿人:王小丽 字号: 默认 | | 超大 打印

一、简介

reduce() 对数组每个元素执行一次由您提供的reduce函数(升序执行),将其结果汇总为单个返回值。

循环遍历能做的,reduce都可以做。比如数组根据元素某个属性求和、数组元素出现次数、数组去重、数组根据某个元素属性分类等等。

arr.reduce((prev, cur, index, arr) => {
  return prev + cur
}, 0)

参数介绍

以下为有无 init 的区别
演示代码

console.log('-------有init---------')
const arr = [1, 2, 3, 4, 5]
const sum = arr.reduce((prev, cur, index, arr) => {
  console.log(prev, cur, index, arr)
  return prev + cur
}, 0)
console.log(arr, sum)
console.log('-------无init---------')
const arr2 = [1, 2, 3, 4, 5]
const sum2 = arr.reduce((prev, cur, index, arr) => {
  console.log(prev, cur, index, arr)
  return prev + cur
})
console.log(arr2, sum2)

输出结果

-------有init---------
0 1 0 [ 1, 2, 3, 4, 5 ]
1 2 1 [ 1, 2, 3, 4, 5 ]
3 3 2 [ 1, 2, 3, 4, 5 ]
6 4 3 [ 1, 2, 3, 4, 5 ]
10 5 4 [ 1, 2, 3, 4, 5 ]
[ 1, 2, 3, 4, 5 ] 15
-------无init---------
1 2 1 [ 1, 2, 3, 4, 5 ]
3 3 2 [ 1, 2, 3, 4, 5 ]
6 4 3 [ 1, 2, 3, 4, 5 ]
10 5 4 [ 1, 2, 3, 4, 5 ]
[ 1, 2, 3, 4, 5 ] 15

二、求和

假如我们想把对商品的价格进行求和
演示代码

// 根据数组对象某个属性求和
const shopList = [
  {
    id: 1,
    name: '苹果',
    isDeleted: false,
    num: 2,
    price: 5999
  },
  {
    id: 2,
    name: '小米',
    isDeleted: false,
    num: 1,
    price: 3999
  },
  {
    id: 3,
    name: '华为',
    isDeleted: true,
    num: 3,
    price: 4999
  }
]
let shopListSum = shopList.reduce((prev, item) => {
  prev += item.num * item.price
  return prev
}, 0)
console.log(shopListSum)

输出结果

30994

三、出现次数

假如我们想计算每件商品的搜索次数
演示代码

// 计算数组元素次数
const nameList = ['xiaomi', 'huawei', 'pingguo', 'xiaomi', 'huawei', 'huawei']
let countedNameObj = nameList.reduce((prev, item) => {
  if (item in prev) {
    prev[item]++
  } else {
    prev[item] = 1
  }
  return prev
}, {})
console.log(countedNameObj)

输出结果

{ xiaomi: 2, huawei: 3, pingguo: 1 }

四、去重

可分为直接去重和排序后去重

1.直接去重
演示代码

const nameList = ['xiaomi', 'huawei', 'pingguo', 'xiaomi', 'huawei', 'huawei']
let newNameList = nameList.reduce((prev, item) => {
  if (prev.indexOf(item) === -1) {
    prev.push(item)
  }
  return prev
}, [])
console.log(newNameList)

输出结果

[ 'xiaomi', 'huawei', 'pingguo' ]

2.排序后去重
演示代码

const numList = [1, 4, 5, 5, 7, 5, 8, 1, 2, 3, 4, 5, 9, 1, 2]
let newNumList = numList.sort().reduce((prev, item) => {
  if (prev.length === 0 || prev[prev.length - 1] !== item) {
    prev.push(item)
  }
  return prev
}, [])
console.log(newNumList)

输出结果

[1, 2, 3, 4, 5, 7, 8, 9]

五、分类

假如我们想把商品数组里面上架和未上架的商品分类
演示代码

// 根据数组对象某个属性分类
const shopList = [
  {
    id: 1,
    name: '苹果',
    isDeleted: false
  },
  {
    id: 2,
    name: '小米',
    isDeleted: false
  },
  {
    id: 3,
    name: '华为',
    isDeleted: true
  }
]
function groupBy(list, pros) {
  return list.reduce((prev, item) => {
    let key = item[pros]
    if (!prev[key]) {
      prev[key] = []
    }
    prev[key].push(item)
    return prev
  }, {})
}
let myShopList = groupBy(shopList, 'isDeleted')
console.log(myShopList)

输出结果

{
  false: [
    { id: 1, name: '苹果', isDeleted: false },
    { id: 2, name: '小米', isDeleted: false }
  ],
  true: [ { id: 3, name: '华为', isDeleted: true } ]
}