const arr = [3, 5, 1, 2];
const a = arr.reduce((t, v) => t + v);
const b = arr.reduce((t, v) => {
return t + v;
})
const c = arr.reduce((t, v) =>(t.unshift(v),t), [])
const d = arr.reduceRight((t, v) =>(t.push(v), t), [])
const e = arr.reduce((t, v) => {
return [...t, v * 2]
}, [])
const e2 = arr.reduce((t, v) => [...t, v * 2], [])
const f = arr.reduce((t, v) => {
return v > 1 ? [...t, v] : t;
},[])
const g = arr.reduce((t, v) => v > 1 ? [...t, v] : t, [])
const scores = [
{ score: 45, subject: "chinese" },
{ score: 90, subject: "math" },
{ score: 60, subject: "english" }
];
const h = scores.reduce((t, v) => t || v.score >= 60, true);
const i = scores.reduce((t, v) => t && v.score >= 60, true)
const j = arr.reduce((t, v) => {
t[t.length - 1].length === 2 ? t.push([v]) : t[t.length - 1].push(v);
return t;
}, [[]])
function help(arr = [], size = 1){
return arr.length ? arr.reduce((t, v) => (t[t.length - 1].length == size ? t.push([v]) : t[t.length - 1].push(v), t),[[]]) : [];
}
//console.log(help(arr, 2))
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [2, 3, 6];
function difference(arr1 = [], arr2 = []) {
return arr1.reduce((t, v) => {
if(!arr2.includes(v)){
t.push(v)
}
return t;
}, [])
}
function difference1(arr1 = [], arr2 = []) {
return arr1.reduce((t, v) => (!arr2.includes(v) && t.push(v), t), [])
}
let ans = difference1(arr1, arr2);
const arr3 = [0, 1, [2, 3], [4, 5, [6, 7]], [8, [9, 10, [11, 12]]]];
function flat(arr = []) {
return arr.reduce((t, v) => t.concat(Array.isArray(v) ? flat(v) : v),[])
}
function flat1(arr = []) {
return arr.reduce((t, v) => {
return t.concat(Array.isArray(v) ? flat1(v) : v)
},[])
}
let res = flat1(arr3)
function flat2(arr = []){
return arr.toString().split(',').map(i => Number(i))
}
const arr4 = [2, 1, 0, 3, 2, 1, 2];
let k = arr4.reduce((t, v) => t.includes(v) ? t : [...t, v], [])
let l = arr4.reduce((t, v) => t > v ? t: v);
let m = arr4.reduce((t, v) => t < v ? t: v);
let n = arr4.reduce((t, v) => ((t[v] || 0) + 1, t), {})
function position(arr = [], val) {
return arr.reduce((t, v, i) => (v === val && t.push(i), t), [])
}
let o = position(arr4, 2)
function group(arr = [], key){
return key ? arr.reduce((t, v) => {
if(!t[v[key]]){
t[v[key]] = [];
t[v[key]].push(v);
}
return t;
},{}):{};
}
const arr5 = [
{ 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 }
];
let p = group(arr5)
const str = 'reduce考的好频繁'
let strToArr = str.split('')
let q = strToArr.reduceRight((t, v) => {
return t + v;
})
function fibonacci(len = 2) {
let arr = [...new Array(len).keys()];
return arr.reduce((t, v, i) => {
if(i > 1){
t.push(t[i - 1] + t[i - 2])
}
return t;
},[0, 1])
}
let r = fibonacci(10);
function getKeys(obj = {}, keys = {}){
return Object.keys(obj).reduce((t, v) => {
if(keys.includes(v)){
t[v] = obj[v]
}
return t;
}, {})
}
const target = { a: 1, b: 2, c: 3, d: 4 };
const keyword = ["a", "d"];
let s = getKeys(target, keyword)
const people = [
{ area: "GZ", name: "YZW", age: 27 },
{ area: "SZ", name: "TYJ", age: 25 }
];
const t = people.reduce((t, v) => {
const {name, ...rest} = people;
t[name] = rest;
return t;
})
console.log(t)
console