ECMAScript5 新增了十个数组方法,这些方法只有在ie9及以上浏览器中可以被使用,下面是对于这些方法的模拟实现。
一、Array.isArray(element)
该方法用于判断传入的对象是否为数组类型,返回true和false。
Array.newIsArray = function(element){
return Object.prototype.toString.call(element).slice(8,-1).toLocaleLowerCase() === 'array';
}
二、.indexOf(element)
该方法用于查找传入对象在数组中的位置,并返回该位置,若没有找到则返回-1,该方法不能用于寻找undefined。
indexOf方法可以和~符配合使用。按位运算符~会将传入数字取反并减一,所以-1就会变成0,这时候把它放在判断条件中会被隐式转换为false。
Array.prototype.newIndexOf = function(element){
var index = -1;
for(var i = 0; i < this.length; i++){
if(this[i] === element && this[i] !== undefined){
index = i;
break;
}
}
return index;
};
var a = [1,2,3,4,,,5];
console.log(a.newIndexOf(undefined));
三、lastIndexOf(element)
该方法与indexOf(element)作用和返回值相同,唯一不同的地方是它是从右向左寻找。
Array.prototype.newLastIndexOf = function(element){
var index = -1;
for(var i = this.length - 1; i >= 0; i--){
if(this[i] === element && this[i] !== undefined){
index = i;
break;
}
}
return index;
};
var a = [1,2,3,4,5,2,,,3];
console.log(a.newLastIndexOf(undefined));
四、forEach(function(element, index, array){})
遍历数组,参数为一个回调函数,有三个传参:当前元素、当前元素索引、整个数组,该方法会跳过保留缺失成员,不会破坏原数组。
Array.prototype.newForEach = function(fn){
for(var i = 0; i < this.length; i++){
if(i in this){
fn(this[i], i, this);
}
}
};
var a = [1,2,3,undefined,undefined,4,5,2,3];
a.forEach(function(e, i, arr){
console.log(e, i, arr);
})
五、every(function(element, index, array){})
使用传入的回调函数遍历数组,当所有回调都返回true时,every方法返回true,否则返回false。该方法会跳过保留缺失成员,不会破坏原数组。
Array.prototype.newEvery = function(fn){
var status = true;
for(var i = 0; i < this.length; i++){
if(i in this){
if(!(status = !!fn(this[i], i, this))){
break;
}
}
}
return status;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newEvery(function(){
console.log(arguments);
return 1;
}));
六、some(function(element, index, array){})
使用传入的回调函数遍历数组,当有回调返回true时,some方法返回true,否则返回false。该方法会跳过保留缺失成员,不会破坏原数组。
Array.prototype.newSome = function(fn){
var status = false;
for(var i = 0; i < this.length; i++){
if(i in this){
if(status = !!fn(this[i], i, this)){
break;
}
}
}
return status;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newSome(function(){
console.log(arguments);
return 0;
}));
七、map(function(element, index, array){})
使用传入的回调函数遍历数组,使用遍历数组返回的内容组成一个新的数组,该方法会跳过空元素,但是最终结果保留缺失成员的位置,不会破坏原数组。
Array.prototype.newMap = function(fn){
var array = new Array(this.length);
for(var i = 0; i < this.length; i++){
if(i in this){
array[i] = fn(this[i], i, this);
}
}
return array;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newMap(function(element, index, array){
console.log(arguments);
return element;
}))
八、filter(function(element, index, array){})
使用传入的回调函数遍历数组,最终返回一个新数组,该数组中包含所有回调函数返回true的元素,不会破坏原数组。
Array.prototype.newFilter = function(fn){
var array = [];
for(var i = 0; i < this.length; i++){
if((i in this) && fn(this[i], i, this)){
array.push(this[i]);
}
}
return array;
};
var a = [1,2,3,4,5,2,undefined,,3];
console.log(a.newFilter(function(element, index, array){
console.log(arguments);
return element;
}))
九、reduce(function(accumulator, currentValue, currentIndex, array){})
使用传入的回调函数遍历数组,返回最后一个回调函数调用的返回值,跳过缺失成员,不会破坏原数组。
Array.prototype.newReduce = function(fn){
if(this.length === 0){
throw new TypeError('Reduce of empty array with no initial value');
}
var result;
for(var i = 0; i < this.length; i++){
if(i in this){
if(!result){
result = this[i];
}else{
result = fn(result, this[i], i, this);
}
}
}
return result;
};
var a = [,,1,2,3,4,,6,7];
console.log(a.newReduce(function(a,b){
console.log(arguments);
return a + b;
}))
十、reduceRight(function(accumulator, currentValue, currentIndex, array){})
该方法作用于reduce相同,唯一区别是它是从右往左开始遍历。
Array.prototype.newReduceRight = function(fn){
if(this.length === 0){
throw new TypeError('Reduce of empty array with no initial value');
}
var result;
for(var i = this.length - 1; i >= 0; i--){
if(i in this){
if(!result){
result = this[i];
}else{
result = fn(result, this[i], i, this);
}
}
}
return result;
};
var a = [,,1,2,3,4,,6,7];
console.log(a.newReduceRight(function(a,b){
console.log(arguments);
return a + b;
}))
以上所述是小编给大家介绍的JS模拟实现ECMAScript5新增的数组方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
ecmascript,数组
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]