let testArr = [1,2,3,4]
Array.prototype.reduceFilter = function (callback) {
return this.reduce((acc, cur, index, array) => {
if (callback(cur, index, array)) {
acc.push(cur)
}
return acc
}, [])
}
let c = testArr.reduceFilter(item => item % 2 == 0)
console.log(c)
const testObj = [{a:1} , {a:2},{a:3},{a:4}]
Array.prototype.redecuFind = function(callback){
return this.reduce((acc,cur,index,array)=>{
if(callback(cur,index,array)){
if(acc instanceof Array && acc.length ==0){
acc = cur
}
}
if((index == array.length-1) && acc instanceof Array && acc.length==0){
acc = undefined
}
return acc
},[])
}
console.log(testObj.redecuFind(item => item.a%2 ==0))
const testArr1 = [[1,2],[3,4],[5,6,7]]
let A = testArr1.reduce((acc,cur)=>{
return acc.concat(cur)
},[])
console.log(A)
const testArr2 = [1,3,4,1,3,2,9,8,5,3,2,0,12,10]
let B = testArr2.reduce((acc,cur)=>{
if(!(cur in acc)){
acc[cur] = 1
}else{
acc[cur]+=1
}
return acc
},{})
console.log(B)
const bills = [
{ type: 'shop', momey: 223 },
{ type: 'study', momey: 341 },
{ type: 'shop', momey: 821 },
{ type: 'transfer', momey: 821 },
{ type: 'study', momey: 821 }
];
let C = bills.reduce((acc, cur) => {
if (!acc[cur.type]) {
acc[cur.type] = [];
}
acc[cur.type].push(cur)
return acc
}, {})
console.log(C)
const testArr4 = [1,2,2,3,4,4,5,5,5,6,7]
testArr4.reduce((acc, cur) => {
if (!(acc.includes(cur))) {
acc.push(cur)
}
return acc
}, [])
const testArr5 = [
{ age: 20 },
{ age: 21 },
{ age: 22 }
]
testArr5.reduce((acc, cur) => {
if (!acc) {
acc = cur
return acc
}
if (acc.age < cur.age) {
acc = cur
return acc
}
return acc
}, 0)
console