二维数组转一维
// 二维数组转一维数组
const flattened = [[0,1],[2,3],[4,5]].reduce((a,b) => a.concat(b), [])
console.log(`flattened is ${flattened}`)
数组中元素出现次数统计
// 计算数组中每个元素出现的次数
const names = ['a', 'b', 'a', 'c', 'a', 'c']
const counterNames = names.reduce((allNames, name) => {
if (name in allNames) {
allNames[name] += 1
} else {
allNames[name] = 1
}
return allNames
}, {})
console.log(`counterNames is`)
console.log(counterNames)