求问!web前端 2016阿里应届生编程题 js题目

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

 约定: title数据类型为String
 userId为主键,数据类型为Number 
```
var data = [ {userId: 8, title: 'title1'}, {userId: 11, title: 'other'}, {userId: 15, title: null}, {userId: 19, title: 'title2'} ];
var find = function(origin){ //your code are here... } //查找data中,符合条件的数据,并进行排序
测试用例:
 var result = find(data).where({"title": /\d$/}).orderBy('userId', 'desc'); console.log(result); // [{ userId: 19, title: 'title2'}, { userId: 8, title: 'title1' }];

下面是我的写法:


这个写法输出正确。感觉题目的意思不是这样子。请教各位大神,有更好的写法吗?


#阿里巴巴##前端工程师#
全部评论
function xx(_dat){ function newData() { this.data = []; this.init = function(_d) { this.data = _d; return this; }; this.where = function(_c) { return (new newData()).init(this.data.filter(function(_item) { return _item.value.match(_c) })); }; this.orderBy = function(_k,_way) { if(_way == "desc"){ return this.data.sort(function(a,b) { return b[_k] - a[_k]; }); }else{ return this.data.sort(function(a,b) { return a[_k] - b[_k]; }); } }; } return (new newData()).init(_dat); }
点赞 回复 分享
发布于 2016-09-09 17:22

相关推荐

好在哪里了?我请问了?
仁者伍敌:活着的人都说好,帮我盖上棺材盖谢谢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务