js链式调用(重点原型链)

题目:提供了一个数组结构的 data,要求实现一个 query 方法,返回一个新的数组,query 方法内部有 过滤、排序、分组 等操作,并且支持链式调用,调用最终的 execute 方法返回结果:

//  js
// const result = query(list)
//   .where(item => item.age > 18)
//   .sortBy('id')
//   .groupBy('name')
//   .execute();
// console.log(result);
const listPro = [
		{
			name: '章三',
			id: 5,
			age: 20
		},
		{
			name: '章五',
			id: 2,
			age: 14
		},
		{
			name: '章三',
			id: 8,
			age: 14
		},
		{
			name: '章三',
			id: 1,
			age: 22
		},
		{
			name: '章五',
			id: 3,
			age: 19
		}
	]

解题思路一:构造函数:

 function Fun1 (data){
        this.data = data
           // this.where = function(cd){
	        // 	this.data =  this.data.filter(cd)
	        // 	return this
    	// }
    }
    Fun1.prototype.where = function(cd){
        this.data =  this.data.filter(cd)
        return this
    }
    Fun1.prototype.sortBy = function(key){
        this.data = this.data.sort((a,b)=>a[key]-b[key])
        return this
    }
    Fun1.prototype.groupBy = function(key){
        const map = new Map();
            this.data.forEach((d) => {
                if (map.has(d[key])) {
                    map.get(d[key]).push(d);
                } else {
                    map.set(d[key], [d]);
                }
            });

            this.data = Array.from(map.values());
            return this;
    }
    Fun1.prototype.execute = function(){
            return this.data
    }
    function query (listPro){
        return new Fun1(listPro)
    }
    const result = query(listPro)
    .where(item => item.age > 18)
    .sortBy('id')
    .groupBy('name')
    .execute();
    console.log(result);

解题思路二:class

 class fun1 {
        constructor(data){
            this.data = data
        }

        where(fun) {
            this.data = this.data.filter(fun)
            return this
        }
        sortBy(key){
        // for(let i=0; i<this.data.length-1; i++){
	        //     //每一轮比较要比多少次
	        //     for(let j=0; j<this.data.length-1-i; j++){
            //         //如果第一个比第二个大,就交换他们两个位置
            //         if(this.data[j][key]<this.data[j+1][key]){
            //             var temp = this.data[j];
            //             this.data[j] =this.data[j+1];
            //             this.data[j+1] = temp;
            //         }
	        //     }    
            // }
             this.data = this.data.sort((a,b)=>a[key]-b[key])
            return this
        }
        groupBy(key){
            const map = new Map();
            this.data.forEach((d) => {
                if (map.has(d[key])) {
                    map.get(d[key]).push(d);
                } else {
                    map.set(d[key], [d]);
                }
            });

            this.data = Array.from(map.values());
            return this;
        }
        execute(){
            console.log(this.data)
            return this
        }
    }
    function query (listPro){
        return new fun1(listPro)
    }
    const result = query(listPro)
    .where(item => item.age > 18)
    .sortBy('id')
    .groupBy('name')
    .execute();
    console.log(result);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值