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