function where(collection, source) {
var arr = [];
// What's in a name?
var keys = Object.keys(source);
arr = collection.filter(function(item){
for (var i = 0; i < keys.length; i ++){
if(!item.hasOwnProperty(keys[i]) || item[keys[i]] !== source[keys[i]]){
return false;
}
}
return true;
}); return arr;
} where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

另外一种方法

function where(collection, source) {
var arr = [];
// What's in a name?
var keys = Object.keys(source);
arr = collection.filter(function(item){
return keys.every(function(key){
return item.hasOwnProperty(key) && item[key] === source[key];
});
});
return arr;
} where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

Where art thou的更多相关文章

  1. freeCodeCamp:Where art thou

    写一个 function,它遍历一个对象数组(第一个参数)并返回一个包含相匹配的属性-值对(第二个参数)的所有对象的数组.如果返回的数组中包含 source 对象的属性-值对,那么此对象的每一个属性- ...

  2. Where art thou-freecodecamp算法题目

    Where art thou 1.要求 写一个 function,它遍历一个对象数组(第一个参数)并返回一个包含相匹配的属性-值对(第二个参数)的所有对象的数组. 如果返回的数组中包含 source ...

  3. 每天写点shell——read的用法

    1.read基本读取 #!/bin/bash #testing the read command echo -n "Enter you name:" #echo -n 让用户直接在 ...

  4. XMPP学习——3、XMPP协议学习补充

    流基础 两个基本概念,使得XMPP实体之间的小的结构化信息有效载荷能快速地进行异步交换:XML流和XML节.这些术语的定义如下. XML流的定义: XML流是一个容器,用于任何两个实体通过网络进行XM ...

  5. FreeCodeCamp 中级算法(个人向)

    freecodecamp 中级算法地址戳这里 Sum All Numbers in a Range 我们会传递给你一个包含两个数字的数组.返回这两个数字和它们之间所有数字的和. function su ...

  6. Help Me Escape (ZOJ 3640)

    J - Help Me Escape Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB ...

  7. 开源Jabber(XMPP) IM服务器介绍

    一.摘要 这是我粗略读了一遍Jabber协议和相关技术文章后的产物,有些地方不一定准确.在文章中引用的一些代码来自www.jabber.org上的文章. 二. 什么是Jabber    Jabber就 ...

  8. XMPP 初探

    最近刚好有机会碰到XMPP,把一些学习心得记录在这边. XMPP(Extensible Messageing and Presence Protocol)是一种IM的通讯协定,其前身为Jabber,后 ...

  9. The Sorrows of Young Werther

    The Sorrows of Young Werther J.W. von Goethe Thomas Carlyle and R.D. Boylan Edited by Nathen Haskell ...

随机推荐

  1. Mac OS X使用快捷键改善窗口管理的六个方法

    http://www.macx.cn/thread-2085916-1-1.html 窗口全屏 ctrl+command+f

  2. C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)

    1.  主版本模板类 首先我们来看一段初学者都能看懂,应用了模板的程序: 1 #include <iostream> 2 using namespace std; 3 4 template ...

  3. php返回json,xml,JSONP等格式的数据

    php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...

  4. TCP/UDP的接收包方式

    UDP udp不是流式的,每次接收一个包,长度不超过(65535-28,总包长65535字节,包头28字节).所以UDP方式下不需要填写任何参数直接调用 $client->recv() 即可.注 ...

  5. mysql 数据库乱码解决

    mysql 数据库乱码解决, 进入前加入 set names 'utf8'  即可.

  6. [ActionScript 3.0] AS3.0 涂鸦及擦除功能,撤销重做步骤记录实例

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BlendMo ...

  7. NSIS打包(一)常用概念简介

    1.NSIS简介 官网:http://sourceforge.net/projects/nsis/ 维基百科: http://zh.wikipedia.org/wiki/Nullsoft%E8%85% ...

  8. Git服务器、http协议及XCode

    本来费了老鼻子牛劲搭好了SVN,可以通过web进行访问,也弄好了eclipse和XCode,结果几个开发的同事说要上git,悲了个催,又开始折腾git. 因为公司只有一个公网的http出口,因此开始了 ...

  9. ctags+cscope

    a opensource study website http://www.lanedo.com/2013/the-hidden-pearls-of-tracker-2/ http://www.lan ...

  10. SpringMVC整合MongoDB开发 架构搭建

    系统环境: 操作系统:  windows 7 数 据 库:  mongodb2.0.6 驱 动 包: Spring3.1.2 + mongodb2.7.3 + spring-data-mongodb1 ...