reduce的高级用法

01:代替map和filter
const arr = [0, 1, 2, 3];
// 代替map:[0, 2, 4, 6]
const arrA = arr.map(item => item * 2);
const arrB = arr.reduce((items, item) => {
    return [...items, item * 2]
}, []);
 
// 代替filter:[2, 3]
const A = arr.filter(item => {
    return item > 1
});
const B = arr.reduce((items, item) => {
    return item > 1 ? [...items, item] : t
}, []);
 
// 代替map和filter:[4, 6]
const C = arr.map(item => {
    return item * 2
}).filter(item => {
    return item > 2
});
const D = arr.reduce((items, item) => {
    return item * 2 > 2 ? [...items, item * 2] : items
}, []);
02:数组分割 function Chunk(arr = [], size = 1) { return arr.length ? arr.reduce((items, item) => { return items[items.length - 1].length === size ? items.push([item]) : items[items.length - 1].push(item) , items }, [[]]) : []; } const arr = [1, 2, 3, 4, 5]; Chunk(arr, 2); // [[1, 2], [3, 4], [5]] 03:数组过滤 function Difference(arr = [], oarr = []) { return arr.reduce((items, item) => { return (!oarr.includes(item) && items.push(item), items) }, []); } const arr1 = [1, 2, 3, 4, 5]; const arr2 = [2, 3, 6] Difference(arr1, arr2); // [1, 4, 5] 04:数组填充 function Fill(arr=[],val="",start=0,end=arr.length) { if (start < 0 || start >= end || end > arr.length){ return arr }; return [ ...arr.slice(0, start), ...arr.slice(start, end).reduce((items, item) => { return (items.push(val || item), items) }, []), ...arr.slice(end, arr.length) ]; } const arr = [0, 1, 2, 3, 4, 5, 6]; Fill(arr, "aaa", 2, 5); // [0, 1, "aaa", "aaa", "aaa", 5, 6] 05:数组扁平 function Flat(arr = []) { return arr.reduce((items, item) => { return items.concat( Array.isArray(item) ? Flat(item) : item ) }, []) } 06:数组去重 function Uniq(arr = []) { return arr.reduce((items, item) => { return items.includes(item) ? items : [...items, item] ,[] }); } const arr = [2, 1, 0, 3, 2, 1, 2]; Uniq(arr); // [2, 1, 0, 3] 08:数组最大最小值 function Max(arr = []) { return arr.reduce((items, item) => { return items > item ? items : item }); } function Min(arr = []) { return arr.reduce((items, item) => { return items < item ? items : item }); } const arr = [12, 45, 21, 65, 38, 76, 108, 43]; Max(arr); // 108 Min(arr); // 12 09:数组成员独立拆解 function Unzip(arr = []) { return arr.reduce( (items, item) => (item.forEach((w, i) => items[i].push(w)), items), Array.from({ length: Math.max(...arr.map(item => item.length)) }).map(v => []) ); } const arr = [["a", 1, true], ["b", 2, false]]; Unzip(arr); // [["a", "b"], [1, 2], [true, false]] 10:对数组成员个数进行统计 此方法是字符统计和单词统计的原理,入参时把字符串处理成数组即可 function Count(arr = []) { return arr.reduce((items, item) => { return items[item] = (items[item] || 0) + 1, items }, {}); } const arr = [0, 1, 1, 2, 2, 2]; Count(arr); // { 0: 1, 1: 2, 2: 3 } 11:对数组成员特性进行分组 function Group(arr = [], key) { return key ? arr.reduce((t, v) => ( !t[v[key]] && (t[v[key]] = []), t[v[key]].push(v), t ), {}) : {}; } const arr = [ { area: "GZ", name: "YZW", age: 27 }, { area: "GZ", name: "TYJ", age: 25 }, { area: "SZ", name: "AAA", age: 23 }, { area: "FS", name: "BBB", age: 21 }, { area: "SZ", name: "CCC", age: 19 } ]; // 以地区area作为分组依据 Group(arr, "area"); //{ GZ: Array(2), SZ: Array(2), FS: Array(1) } 12:对数组成员包含的关键字进行统计 function Keyword(arr = [], keys = []) { return keys.reduce((t, v) => ( arr.some(w => w.includes(v)) && t.push(v), t), []); } const text = [ "今天天气真好,我想出去钓鱼", "我一边看电视,一边写作业", "小明喜欢同桌的小红,又喜欢后桌的小君,真TM花心", "最近上班喜欢摸鱼的人实在太多了,代码不好好写,在想入非非" ]; const keyword = ["偷懒", "喜欢", "睡觉", "摸鱼", "真好", "一边", "明天"]; Keyword(text, keyword); // ["喜欢", "摸鱼", "真好", "一边"] 13:字符串翻转 function ReverseStr(str = "") { return str.split("").reduceRight((items, item) => items + item); } const str = "reduce最牛逼"; ReverseStr(str); // "逼牛最ecuder" 14:累加累乘 function Accumulation(...vals) { return vals.reduce((t, v) => t + v, 0); } function Multiplication(...vals) { return vals.reduce((t, v) => t * v, 1); } Accumulation(1, 2, 3, 4, 5); // 15 Multiplication(1, 2, 3, 4, 5); // 120
  15:权重求和
const scores = [
    { score: 90, subject: "chinese", weight: 0.5 },
    { score: 95, subject: "math", weight: 0.3 },
    { score: 85, subject: "english", weight: 0.2 }
];
const result = scores.reduce((t, v) => t + v.score * v.weight, 0); // 90.5
16:返回对象指定的键值 function GetKeys(obj = {}, keys = []) { return Object.keys(obj).reduce((items, item) => ( keys.includes(item) && (items[item] = obj[item]), items ), {}); } const target = { a: 1, b: 2, c: 3, d: 4 }; const keyword = ["a", "d"]; GetKeys(target, keyword); // { a: 1, d: 4 }
原文地址:https://www.cnblogs.com/zzkxjh/p/13632619.html