//1.1.累加和累乘
let arr1 = [1, 2, 3, 4]
function 加(arr = []) {
return arr.reduce((total, item) => total + item)
}
function 乘(arr = []) {
return arr.reduce((total, item) => total * item)
}
console.log(加(arr1))
console.log(乘(arr1))
//1.2.权重求和&权重随机抽取
let arr = [{ name: '张三', a: 20, b: 100 }, { name: '李四', a: 30, b: 33 }, { name: '王五', a: 50, b: 20 }]
function arrAdd(arr) {
return arr.reduce((total, item) => (total + item.a * item.b), 0)
}
console.log(arrAdd(arr));
let num = arr.reduce((total, item) => {
item.start = total
return item.end = item.start + item.a
}, 0)
let 随机数 = Math.ceil(Math.random() * num)
let 随机对象 = arr.find((item) => {
return item.start <= 随机数 && item.end > 随机数
})
console.log(随机对象)
//1.3.代替reverse()方法
let arr3 = [1, 2, 4, 5, 7, 6]
function 倒转方法(arr = []) {
return arr.reduceRight((total, item) => (total.push(item), total), [])
}
console.log(倒转方法(arr3))
// 1.4.代替了map和filter
let arr4 = [1, 2, 3, 4, 5]
function map(arr = []) {
return arr.reduce((total, item) => [...total, item * 2], [])
}
console.log(map(arr4))
function filter(arr = []) {
return arr.reduce((total, item) => (item > 2 ? (total.push(item), total) : total), [])
}
console.log(filter(arr4))
//1.5.代替some 和every 循环
let 学习 = [{ name: '语文', number: 78 }, { name: '数学', number: 90 }, { name: '英语', number: 60 }]
function some(arr = []) {
return arr.reduce((total, item) => total || item.number >= 70, false)
}
console.log('是否有一门功课及格', some(学习))
function every(arr = []) {
return arr.reduce((total, item) => total && item.number >= 60, true)
}
console.log('是否全部功课及格', every(学习))
//1.6.数组分割
let xArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
function arrSplice(arr = [], num = 1) {
return arr.reduce((total, item) => (total.length === num ? total.push([item]) : total[total.length - 1].push(item), total), [[]])
}
console.log(arrSplice(xArr, 4))
// 2.1.数组过滤
let arr71 = [1, 2, 3, 4]
let arr72 = [3, 4, 5, 6]
function filters(arr1 = [], arr2 = []) {
return arr1.reduce((total, item) => (!arr2.includes(item) ? total.push(item) : total, total), [])
}
console.log('过滤:', filters(arr71, arr72))
// 2.2 数组填充
let arr_2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function 填充(arr = [], i = '', start = 0, end = 0) {
return [
...arr.slice(0, start),
...arr.slice(start, end).reduce((total, item, index) => (total.push(i || item), total), []),
...arr.slice(end, arr.length)
]
}
console.log(填充(arr_2, 'aaa', 2, 5))
//2.3 数组扁平化
let arr_23 = [0, 1, [2, 3], [4, 5, [6, 7]], [8, [9, 10, [11, 12]]]]
function 降维(arr = []) {
return arr.reduce((total, item) => total.concat((Array.isArray(item) ? 降维(item) : item)), [])
}
console.log(降维(arr_23))
//2.4 数组去重
let arr_24 = [1, 1, 3, 4, 2, 3, 4, 2, 5];
function 去重(arr = []) {
return arr.reduce((total, item) => (total.includes(item) ? total : total.push(item), total), [])
}
console.log(去重(arr_24))
//2.5.数组最大值和最小值
let arr_25 = [12, 12, 45, 21, 65, 38, 76, 108, 108, 43];
function 最大(arr = []) {
return arr.reduce((total, item) => total >= item ? total : item)
}
function 最小(arr = []) {
return arr.reduce((total, item) => total <= item ? total : item)
}
console.log(最大(arr_25), 最小(arr_25))
//2.6.数组成员独立拆解
let arr_26 = [["a", 1, true, false, 'x'], ["b", 2]];
function 独立拆解(arr = []) {
let isArr = Array.from({ length: Math.max(...arr.map((i) => i.length)) }).map(i => [])
console.log(isArr)
return arr.reduce((total, item, index) => (item.forEach((i, inx) => total[inx].push(i)), total), isArr)
}
console.log(独立拆解(arr_26));
// 3.1数组成员个数统计
((arr) => {
let obj = arr.reduce((total, item) => (total[item] = (total[item] || 0) + 1, total), {})
return console.log(obj)
})(['s', 's', 't', 'i', 'v', 'i', 't', 's', 2, 1, 2]);
//3.2数组成员位置记录
((arr, num) => {
let 结果 = arr.reduce((total, item, index) => (item == num ? total.push(index) : total, total), [])
return console.log(结果)
})([1, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5], 2);
//3.3数组成员特性分组
((arr, key) => {
const logArr = key ? arr.reduce((total, item) => (!total[item[key]] && (total[item[key]] = []), total[item[key]].push(item), total), {}) : {}
console.log(logArr)
})([{ name: '张三', area: 'AY', age: 26 }, { name: '张四', area: 'AY', age: 27 }, { name: '李四', area: 'XM', age: 22 }, { name: '李五', area: 'XM', age: 21 }, { name: '王五', area: 'ZZ', age: '25' }], 'area');
//3.4 关键字统计
const arr3_4_1 = [
"今天天气真好,我想出去钓鱼",
"我一边看电视,一边写作业",
'明天',
"小明喜欢同桌的小红,又喜欢后桌的小君,真TM花心",
"最近上班喜欢摸鱼的人实在太多了,代码不好好写,在想入非非"
];
const arr3_4_2 = ["偷懒", "喜欢", "睡觉", "摸鱼", "真好", "一边", "明天"];
((arrList, keyArr) => {
let 结果 = keyArr.reduce((total, item) => {
return (arrList.some((i) => i.includes(item)) && total.push(item), total)
}, [])
console.log(结果)
})(arr3_4_1, arr3_4_2);
//3.5字符串翻转
((i) => {
console.log(i.split(''))
let 结果 = i.split('').reduceRight((total, item) => (total += item), '')
console.log(结果)
})('reduce牛逼');
//3.6 数字千分化
function 千分化(num) {
//转换数据类型 小数点以前的数字需要 倒转过来并配合reduceRight
let arr = num.toString().split('.').map((item, index) => {
if (index == 0) return item.split('').reverse()
return item.split('')
})
/**
* @public isDot:判断是否是整数部分 如果是整数部分需要配合reduceRight方法才能从个位数开始计算
*
* */
const number = (i, isDot = false) => i[isDot ? 'reduce' : 'reduceRight']((total, item, index) => total += ((index + isDot) % 3 ? item : `${item},`), '').replace(/^,|,$/g, '')
//图文中的变量[ 下标 ]是为了解决小数点后面的 防止小数点后第一位加上['','']
return console.log(arr.length == 2 ? `${number(arr[0])}.${number(arr[1], true)}` : number(arr[0]))
}
千分化(1000)
千分化(1234.00)
千分化(0.1234)
千分化(1234.5678);
// 4.1斐波那契数列
(i => {
console.log(i)
let arr = [...new Array(i).keys()]
let 结果 = arr.reduce((total, item, index) => (index > 1 && total.push(total[index - 1] + total[index - 2]), total), [0, 1])
console.log(结果)
})(12);
//4.2 url反序列化
(url => {
let arr = url.split('?')[1].split('&').reduce((total, item) => {
let [key, value] = item.split('=')
total[key] = value
return total
}, {})
console.log(arr)
})('https://www.baidu.com?age=25&name=ZB&love=girl');
//4.3 url序列化
((urlHttp, obj = {}) => {
let arr = Object.entries(obj)
let 结果 = arr.reduce((total, item) => (total += `${item[0]}=${item[1]}&`), urlHttp).replace(/&$/g, '')
console.log(结果)
})('http://localhost:8080/?', {
age: 1,
love: 'girl',
name: 'ZB'
});
//4.4返回对象指定的键值对
((obj, ARRLIST) => {
let arrKey = Object.keys(obj)
let 结果 = arrKey.reduce((total, item) => (ARRLIST.includes(item) && (total[item] = obj[item]), total), {})
console.log(结果)
})({ a: 1, b: 2, c: 3, d: 4 }, ['a', 'b']);
// 4.5数组转对象
((arr) => {
let 结果 = arr.reduce((total, item) => {
let {name, ...value} = item
total[name] = value
return total
}, {})
console.log(结果)
})(
[{
area: "AY",
name: "ZB",
age: 26
}, {
area: "XM",
name: "LYF",
age: 22
}]);
console