var data = [{
userId: 8,
title: 'title1'
},
{
userId: 11,
title: 'other'
},
{
userId: 15,
title: null
},
{
userId: 19,
title: 'title2'
}
];
var find = function (origin) {
}
var result = find(data).where({
'title': /\d$/
}).orderBy('userId', 'desc’);
console.log(result);
var find = function (origin) {
return {
data: origin,
where: function (obj) {
for (let key in obj) {
console.log(obj, key)
if (obj.hasOwnProperty(key)) {
this.data = this.data.filter(v => obj[key].test(v[key]))
}
}
return find(this.data)
},
orderBy: function (key, order) {
this.data.sort((a, b) => {
if (order == 'desc') {
return b[key] - a[key]
} else {
return a[key] - b[key]
}
})
return this.data
}
}
}
var result = find(data).where({
'title': /\d$/,
'userId': /9$/
}).orderBy('userId', 'desc’);
console.log(result);