function where(collection, source) {
var arr = [];
// What's in a name?
for (var index = 0; index < collection.length; index++) {
for (var key in collection[index]) {
var count = 0;
for (var key2 in source) {
if (collection[index].hasOwnProperty(key2)) {
if (source[key2] == collection[index][key2]) {
count++;
}
if (count == Object.getOwnPropertyNames(source).length && key == key2) {
arr.push(collection[index]);
}
}
}
}
}
return arr;
}
where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
参考
https://segmentfault.com/a/1190000010866742
对象搜索算法挑战
猜你喜欢
转载自ysbwsx2017.iteye.com/blog/2399967
今日推荐
周排行