JS重构数组⽅法(包括reduce重构数组⽅法)// push:向数组尾部添加⼀个或多个元素,并且返回数组长度,在原数组中操作
function arrayPush(array){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!");
for(var i = 1; i<arguments.length-1; i++ ){
array[array.length] = arguments[i];
}
return array.length;
}
// pop:删除数组尾部的最后⼀个元素,并且返回删除元素的值,在原数组中操组。
function arrayPop(array){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!");
if(array.length == 0) return;
var value = array[array.length-1];
array.length--;js arguments
return value;
}
// shift:删除数组开头的第⼀个元素,并返回删除元素的值,在原数组中操作
function arrayShift(array){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!");
if(array.length === 0) return;
var value = array[0];
for(var i =0; i<array.length-1; i++){
array[i] = array[i+1];
}
array.length--;
return value;
}
// unshift:在数组开头插⼊⼀个或多个元素,然后返回数组的长度,在原数组中操作
function arrayUnshift(array){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!");
for(var i = array.length-1 + arguments.length-1; i>=0; i++){
if(i-(arguments.length-1) >=0 ) array[i] = array[i-(arguments.length-1)];
else array[i] = arguments[i+1];
}
retrun array.length;
}
// concat:连接两个新元素或者是数组,并返回⼀个数组,原数组不变
function arrayConcat(array){
if(!array || strcutor !== Array) throw new TypeError("请输⼊⼀个正确的数组!");
var arr = [];
for(var i = 0; i<arguments.length; i++){
if(arguments[i] && arguments[i].constructor === Array){
for(var j = 0; j<arguments[i].length;j++){
if( j in arguments[i]) arr[arr.length] = arguments[i][j];
else arr.length++;
}
}else arr[arr.length] = arguments[i];
}
return arr;
}
// join:将数组的每个元素⽤⼀个字符连接,默认为",",原数组不变,直接返回字符串
function arrayJoin(array,separator){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!");
if(separator == undefinded) separator = ",";
if(array.length == 0) return;
var str = "";
for(var i =0; i<array.length; i++){
str+=array[i]+separator;
}
return str + array[array.length-1];
}
// slice:获取从数组指定位置到结束位置的内容,返回元素构成新数组,原数组不变
function arraySlice(array,start,end){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!");
if(array.length == 0) return [];
if(start === undefined) start = 0;
if(end === undefined) end= array.length;
start=~~(start);
end=~~(end);
if(start<0) start = (start + array.length < 0 ? 0 : start + array.length);
if(end<0) end= (end+ array.length < 0 ? 0 : end+ array.length);
if(end>array.length) end = array.length;
var arr = [];
for(var i = start; i<end; i++){
arr[arr.length] = array[i];
}
return arr;
}
// splice:插⼊、删除、替换数组中的元素,并且返回被删除的元素组成的新数组,原数组发⽣改变
function arraySplice(array, start, countDelete) {
if (!array || structor !== Array) throw new TypeError("请给⼊正确的数组");
if (start === undefined) return [];
start = ~~start;
if (start < 0) start = (array.length + start < 0 ? 0 : array.length + start);
if (countDelete === undefined) countDelete = array.length - start;
countDelete = ~~countDelete;
if (countDelete < 0) countDelete = 0;
var delArr = [];
var len = start + countDelete;
if (start + countDelete > array.length) len = array.length;
for (var i = start; i < array.length; i++) {
if (i < len) delArr[delArr.length] = array[i];
array[i] = array[i + countDelete];
}
array.length -= len - start;
if (arguments.length <= 3) return delArr;
en = arguments.length - 3;
for (var i = array.length + len - 1; i >= start; i--) {
if (i >= start + len) array[i] = array[i - len]
else array[i] = arguments[arguments.length - len + (i - start)]
}
return delArr;
}
// every:如果⽐较⽐较的每⼀个元素都满⾜条件就返回ture,有⼀个不满⾜就返回-1
function arrayEvery(array,fn){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!");
for(var i =0; i<array.length; i++){
if(i in array && fn(array[i],i,array)) return false;
}
return true;
}
var bool = arrayEvery(array, function(item,index,arr){
return item > 2;
})
使⽤reduce重构
var bool = duce(function(v,t){
if(t<2) v=false;
return v;
},true)
// some:循环数组判断数组中每⼀个是否满⾜条件,如果有⼀个满⾜条件的直接返回true
function arraySome(array){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!"); for(var i = 0; i<array.length; i++){
if(i in array && fn(array[i],i,array) return true;
}
return false;
}
var bool = arraySome(arr,function(item,))
使⽤reduce重构
var bool = duce(function(v,t){
if(t > 2) return v =true;
},false)
// map:遍历数组,返回⼀个由回调函数返回值组成的新数组
function arrayMap(array){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!"); var a = [];
for(var i=0; i<=array.length; i++){
if(i in array) arr[i] = fn(array[i], index, array)
}
return arr;
}
var arr = arrayMap(arr, function(item, index, arr){
return item + 10;
})
使⽤reduce重构
var arr = duce(function(v,t,i){
v[i] = t + 10;
return v'
},[])
// forEach (桥接模式)
function arrayforEach(array,fn){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!"); for(var i = 0; i<=array.length; i++){
if(i in array && fn(array[i])
}
}
var arr = array.arrayforEach(array,function(item, index, arr){
console.log(item,index,arr);
return item;
})
// fill:使⽤固定值填充数组
function arrayFill(array,fn){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!");
for(var i = 0; i<array.length; i++){
if(i in array && fn(array[i])) array[i] = array[i];
}
return array;
}
arrayFill(array,function(item){
retrun item = 1;
})
// filter:筛选 将数组中每个元素判断是否满⾜条件,如果满⾜则筛选出来在新数组中返回
function arrayFilter(array,fn){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!");
var arr =[];
for(var i=0; i<array.length; i++){
if(i in array && fn(array[i])) arr[arr.length] = array[i];
}
return arr;
}
var arr1 = arrayFilter(arr, function(item){
return item > 判断条件;
})
使⽤reduce重构
var arr = duce(function(v,t){
if(t满⾜给定判断条件) v.push(t);
return v;
},[])
// find:获取数组中⼤于给定数值的第⼀个元素
function arrayFind(find){
if(!array || structor !== Array) throw new TypeError("请输⼊⼀个正确的数组!");
for(var i = 0; i<array.length; i++){
if(i in array && fn(array[i])) return array[i];
}
}
var value = arrayFind(array,function(item){
return item > 判断条件;
})
使⽤reduce重构
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论