/*
请实现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) {
//请补充你的代码
let data = origin;
const result = {
where: function (reg) {
const res = data.filter((value) => {
return reg["title"].test(value["title"]);
});
data = res;
return this;
},
orderBy: function (key, method) {
data.sort((a, b) => {
//降序
if (method === "desc") {
return b[key] - a[key];
} else {
return a[key] - b[key];
}
});
return data;
},
};
return result;
};
//查找data中,符合条件的数据,并进行排序
var result = find(data)
.where({
title: /\d$/,
})
.orderBy("userId", "asc");
console.log(result); // [{ userId: 19, title: 'title2'}, { userId: 8, title: 'title1' }];
console