对reduce不熟悉,能够应用的场景,赶紧练练手,jsa代码:
- function test(){//数组当字典用,参数a、i当临时变量用
- let arr = Range('a1').CurrentRegion();
- let res = arr.slice(1).reduce((acc, [a,b,c,d], i) => (
- i = acc[b] ??= acc.push([b,c,0])-1, a = acc[i],
- a[2] += d, a[1].includes(c) || (a[1] += ','+c), acc
- ),[arr[0].slice(1)])
- Range('f6').Resize(res.length, res[0].length).Value2 = res
- }
- function test1(){//使用Set对象去重复
- let arr = Range('a1').CurrentRegion();
- let res = arr.slice(1).reduce((acc, [a,b,c,d], i) => (
- i = acc[b] ??= acc.push([b,c,0])-1,
- a = acc[i], a[2] += d, a[1] +=c, acc
- ),[arr[0].slice(1)])
- res = res.map(([a,b,c],i) => i ? [a,[...new Set(b)].join(","),c] : [a,b,c])
- Range('f6').Resize(res.length, res[0].length).Value2 = res
- }
复制代码
|