对象搜索算法挑战

function where(collection, source) {
var arr = [];
var status = null;
// What's in a name?
for (var i = 0; i < collection.length; i++) {
for(var imp in source){
if (source[imp] !== collection[i][imp]) {
status = 0;
break;
}
status = 1;
}
if(status == 1){
arr.push(collection[i]);
}
}
return arr;
} where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

这样写会不会比使用下面这种好点

function where(collection, source)
{ var arr = [], index = 0; // What's in a name?
// console.log(Object.getOwnPropertyNames(source).length);
//获取对象属性个数
for (; 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;
}

最新文章

  1. js获取屏幕宽高
  2. 【C语言入门教程】4.7 指针的地址分配 - mallocl(), free()
  3. android 九宫加密记事本
  4. HTML 笔记,持续更新
  5. Json 入门例子(form表单)【0】
  6. 从零开始完整Electron桌面开发(1)搭建开发环境
  7. python内置的全局变量
  8. Hadoop的编译
  9. 51 nod 1521 一维战舰 时间复杂度O(n),同 Codeforces 567D. One-Dimensional Battle Ships 有详细注释
  10. 论文笔记:Structure Inference Net: Object Detection Using Scene-Level Context and Instance-Level Relationships
  11. mac控制台快捷键
  12. python 进行机器学习
  13. 路由策略和策略路由 &amp; route-map
  14. gcc5.2版本安装详解
  15. FabricExpress.net supply high quality quilting fabric
  16. Android-DateUtil工具类
  17. 【bzoj1187】 HNOI2007—神奇游乐园
  18. centos7 部署 seafile
  19. [BZOJ2961] 共点圆 [cdq分治+凸包]
  20. [置顶] Python+Django 复选框选择多个 提交只能保存单个

热门文章

  1. java二叉搜索树原理与实现
  2. Web Service与Apache CXF 框架
  3. css设置背景模糊
  4. [SCOI2009]粉刷匠
  5. react context toggleButton demo
  6. 在PHP中使用加密技术
  7. Monitorix:一款面向Linux的轻型系统和网络监测工具
  8. Android MVC模式和MVP模式的区别
  9. Azure 中部署WordPress的方法
  10. 《IT老外在中国》第11期:首次访华的编程巨匠、C#之父Anders