一道面试题的链式调用思考?

请实现find函数,使下列的代码调用正确。

约定:

  • title数据类型为string|null
  • id为主键,数据类型为number
var data = [
    {id: 1, title: 'title1'},
    {id: 2, title: 'other'},
    {id: 3, title: null},
    {id: 4, title: 'title2'}
];

var find = function(origin) {
    //your code are here...
}

//查找data中,符合条件的数据,并进行排序
var result = find(data).where({
    "title": /\d$/
}).orderBy('id', 'desc');

console.log(result); // [{ id: 4, title: 'title2'}, { id: 2, title: 'other' },{id: 1, title: 'title1'}];
阅读 3.8k
3 个回答

有点类似orm里的查询了,以下是我简单写的一个:

class Query {
    constructor (dataList) {
        this.dataList = dataList
    }

    where (conditions) {
        const keyList = Object.keys(conditions)
        const dataList = this.dataList.filter(data => {
            return keyList.every(k => conditions[k].test(data[k]))
        })
        return new Query(dataList)
    }

    orderBy(key, orderType) {
        const func = (a, b) => (a[key] - b[key]) * (orderType == 'desc' ? -1 : 1)
        const list = [...this.dataList].sort(func)
        return new Query(list)
    }
}

function find (dataList) {
    return new Query(dataList)
}

var data = [
    {id: 1, title: 'title1'},
    {id: 2, title: 'other'},
    {id: 3, title: null},
    {id: 4, title: 'title2'}
]

let result = find(data).where({ title: /\d$/ }).orderBy('id', 'desc')
console.log(JSON.stringify(result, null, 2))

一问一答不扩展哈。


function find(dataList) {
    var query = {};

    query.where = function where(condition) {
        var conditionKeys = Object.keys(condition);
        dataList = dataList.filter((item) => {
            return conditionKeys.every(key => condition[key].test(item[key]));
        })
        return query;
    }

    query.orderBy = function orderBy(key, direction) {
        dataList.sort((a, b) => {
            return direction === 'desc' ? b[key] - a[key] : a[key] - b[key];
        })

        return dataList;
    }

    return query;
}

// TEST
var data = [
    {id: 1, title: 'title1'},
    {id: 2, title: 'other'},
    {id: 3, title: null},
    {id: 4, title: 'title2'}
];

//查找data中,符合条件的数据,并进行排序
var result = find(data).where({
    "title": /\d$/
}).orderBy('id', 'desc');

console.log(result);
var find = function(origin) {
                Object.defineProperties(origin, {
                    where: {
                        enumerable: false,
                        value: function(conditions) {
                            const keyList = Object.keys(conditions)
                            const dataList = origin.filter(data => {
                                return keyList.every(k => conditions[k].test(data[k]))
                            });
                            return find(dataList);
                        }
                    },
                    orderBy: {
                        enumerable: false,
                        value: function(key, orderType) {
                            const func = (a, b) => (a[key] - b[key]) * (orderType == 'desc' ? -1 : 1)
                            const list = [...origin].sort(func)
                            return find(list)
                        }
                    }
                })
                return origin;
            }
            var data = [
                {id: 1, title: 'title1'},
                {id: 2, title: 'other'},
                {id: 3, title: null},
                {id: 4, title: 'title2'}
            ]
            let result = find(data).where({ title: /\d$/ }).orderBy('id', 'desc')
            console.log(result);

或者

class Query extends Array {
                where (conditions) {
                    const keyList = Object.keys(conditions)
                    const dataList = this.filter(data => {
                        return keyList.every(k => conditions[k].test(data[k]))
                    })
                    return new Query(...dataList)
                }

                orderBy(key, orderType) {
                    const func = (a, b) => (a[key] - b[key]) * (orderType == 'desc' ? -1 : 1)
                    const list = [...this].sort(func)
                    return new Query(...list)
                }
            }

            function find (dataList) {
                return new Query(...dataList)
            }

            var data = [
                {id: 1, title: 'title1'},
                {id: 2, title: 'other'},
                {id: 3, title: null},
                {id: 4, title: 'title2'}
            ]

            let result = find(data).where({ title: /\d$/ }).orderBy('id', 'desc')
            console.log(result)
推荐问题
宣传栏