前端拿到后端返回的数据后,每每要格式化以知足页面需求。咱们能够使用数组的 reduce()
方法对象数组(数组里面存放的是对象)进行去重。前端
示例代码以下:后端
let data = [ { name: 'tom', id: 1 }, { name: 'jack', id: 2 }, { name: 'sam', id: 3 }, { name: 'mike', id: 1 }, { name: 'amy', id: 2 }, { name: 'eric', id: 3 } ] let hash = {} data = data.reduce((item, next) => { // 根据 id 去重 if(!hash[next.id]) { hash[next.id] = true item.push(next) } return item }, []) console.log(hash) // {1: true, 2: true, 3: true} console.log(data)
去重后结果以下所示:数组
语法:函数
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
reduce()
方法接受两个参数,第一个为回调函数(必填),第二个为初始值(非必填项)callback()
执行数组中每一个值 (若是没有提供 initialValue则第一个值除外
)的函数
initialValue
currentValue
: 数组中正在处理的元素index
(可选): 数组中正在处理的当前元素的索引。 若是提供了initialValue
,则起始索引号为0,不然从索引1起始。array
(可选): 调用reduce()
的数组initialValue
可选
callback
函数时的第一个参数的值。 若是没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错。callback() 回调函数中要有一个返回值.net
描述:code
回调函数第一次执行时,accumulator
和 currentValue
的取值有两种状况:对象
reduce()
时提供了初始值 initialValue
,那么 accumulator
取值为 initialValue()
, currentValue
取数组中的第一个值;initialValue
,那么accumulator
取数组中的第一个值,currentValue
取数组中的第二个值。若是数组为空且没有提供
initialValue
,会抛出TypeError
。若是数组仅有一个元素(不管位置如何)而且没有提供initialValue
, 或者有提供initialValue
可是数组为空,那么此惟一值将被返回而且callback
不会被执行。blog
计算数组中每一个元素出现的次数索引
var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']; var countedNames = names.reduce(function (allNames, name) { if (name in allNames) { allNames[name]++; } else { allNames[name] = 1; } return allNames; }, {}); // countedNames is: // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
数组去重ip
let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd'] let myOrderedArray = myArray.reduce(function (accumulator, currentValue) { if (accumulator.indexOf(currentValue) === -1) { accumulator.push(currentValue) } return accumulator }, []) console.log(myOrderedArray) // ["a", "b", "c", "e", "d"]
参考文章:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce