forEach,map,filter手动封装

forEach,map,filter的封装
接着上文,又封装了数组的常用方法,现在前端找工作越来越要求具有编程能力,难点的可能直接lecode里面随机找一个算法题,简单的可能要求手动封装一个js的api,小编目前也属于比较菜的一部分,先封装几个数组常用的api吧,进入正题了,不扯了。
forEach
Array.prototype.myforEach=function(callback){

         for(let i=0;i<this.length;i++){
              callback(this[i],i,this)
         }
}
let arr=[1,2,3]
arr.myforEach(function(item,index,array){
          console.log(item,index,array);
})

不做过多解释,比较简单,下面看map方法
map
map每次调用回调函数以后return出来的一项就是新数组的每一项
Array.prototype.myMap=function(callback){
let arr=[];
for(let i=0;i<this.length;i++){
arr[i]=callback(this[i],i,this)
}
return arr
}
let arr=[1,2,3]
let newObj= arr.myMap(function(item,index,array){
return item*3;
})
比较简单,不做过多解释
filter
filter和map的区别之处在于filter每次掉用回调函数时候,如果return的是true当前项就会被放在一个新的数组里面。看封装的代码如下
Array.prototype.myFilter=function(callback){
let arr=[];
for(let i=0;i<this.length;i++){
if(callback(this[i],i,this)){
arr.push(this[i])
}
}
return arr
}

let arr=[1,2,3]

let obj= arr.myFilter(function(item,index,array){
if(item>1){
return 1;
}
})
以上就是我对数组常用的几个循环方法的手动封装,希望对你有帮助

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
const relationFunctions = { ancestor: { linked(parent) { this.parent = parent; }, unlinked() { this.parent = null; }, }, descendant: { linked(child) { this.children = this.children || []; this.children.push(child); }, unlinked(child) { this.children = (this.children || []).filter((it) => it !== child); }, }, }; function mapKeys(source, target, map) { Object.keys(map).forEach((key) => { if (source[key]) { target[map[key]] = source[key]; } }); } function makeRelation(options, vantOptions, relation) { const { type, name, linked, unlinked, linkChanged } = relation; const { beforeCreate, destroyed } = vantOptions; if (type === 'descendant') { options.created = function () { beforeCreate && beforeCreate.bind(this)(); this.children = this.children || []; }; options.detached = function () { this.children = []; destroyed && destroyed.bind(this)(); }; } options.relations = Object.assign(options.relations || {}, { [`../${name}/index`]: { type, linked(node) { relationFunctions[type].linked.bind(this)(node); linked && linked.bind(this)(node); }, linkChanged(node) { linkChanged && linkChanged.bind(this)(node); }, unlinked(node) { relationFunctions[type].unlinked.bind(this)(node); unlinked && unlinked.bind(this)(node); }, }, }); } function VantComponent(vantOptions = {}) { const options = {}; mapKeys(vantOptions, options, { data: 'data', props: 'properties', mixins: 'behaviors', methods: 'methods', beforeCreate: 'created', created: 'attached', mounted: 'ready', relations: 'relations', destroyed: 'detached', classes: 'externalClasses', }); const { relation } = vantOptions; if (relation) { makeRelation(options, vantOptions, relation); } // add default externalClasses options.externalClasses = options.externalClasses || []; options.externalClasses.push('custom-class'); // add default behaviors options.behaviors = options.behaviors || []; options.behaviors.push(basic); // map field to form-field behavior if (vantOptions.field) { options.behaviors.push('wx://form-field'); } if (options.properties) { Object.keys(options.properties).forEach((name) => { if (Array.isArray(options.properties[name])) { // miniprogram do not allow multi type options.properties[name] = null; } }); } // add default options options.options = { multipleSlots: true, addGlobalClass: true, }; Component(options); } export { VantComponent };
最新发布
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值