0101
function map(arr, iterate){
var arr2 = []
arr.forEach(function(value, index, arr){
arr2.push(iterate(value,index,arr))
})
return arr2
}
0102
function find(arr, predicate){
for(var index = 0; index<arr.length; index++){
if(predicate(arr[index], index, arr)) return arr[index]
}
}
0103
function filter(list, predicate) {
var results = [];
for (var index = 0; index < list.length; index++) {
if (predicate(list[index], index, list)) results.push(list[index]);
}
return results;
}
0104
function reduce(arr, iteratee, initValue){
var tmpArr = (initValue === undefined ? [] : [initValue]).concat(arr)
while(tmpArr.length > 1){
tmpArr.splice(0, 2, iteratee(tmpArr[0], tmpArr[1]))
}
return tmpArr[0]
}
0105
function flatten(arr){
return arr.reduce(function(initArr, currentArr){
return initArr.concat(Array.isArray(currentArr)?flatten(currentArr):currentArr)
}, [])
}
//或者
function flatten2(arr){
var newArr = []
function _flat(arr){
arr.forEach(val=>{
if(Array.isArray(val)){
_flat(val)
}else{
newArr.push(val)
}
})
}
_flat(arr)
return newArr
}
0201
function byFeild(field){
return function(user1, user2){
return user1[field] > user2[field]
}
}
0202
Array.prototype.equal = function(arr){
return JSON.stringify(this) === JSON.stringify(arr)
}
0203
Array.prototype.repeat = function(n){
var result = []
for(var i=0; i<n; i++){
result = result.concat(this)
}
return result
}
0204
function deepCopy(arr){
return JSON.parse(JSON.stringify(arr))
}
0205
function copyArray(arr){
var cacheOrigin = []
var cacheTarget = []
function copy(arr){
var newArr = []
cacheOrigin.push(arr)
cacheTarget.push(newArr)
for(var i=0; i<arr.length; i++){
if(Array.isArray(arr[i])){
if(cacheOrigin.indexOf(arr[i]) > -1){
newArr.push(cacheTarget[cacheOrigin.indexOf(arr[i])])
}else{
newArr.push(copy(arr[i]))
}
}else{
newArr.push(arr[i])
}
}
return newArr
}
return copy(arr)
}