--- id: a8e512fbe388ac2f9198f0fa title: 找出包含特定键值对的对象 challengeType: 5 forumTopicId: 16092 dashedName: wherefore-art-thou --- # --description-- 创建一个查看对象数组(第一个参数)的函数,并返回具有匹配的名称和值对的所有对象的数组(第二个参数)。 如果要包含在返回的数组中,则源对象的每个名称和值对都必须存在于集合中的对象中。 比如,如果第一个参数是 `[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }]`,第二个参数是 `{ last: "Capulet" }`。 # --hints-- `whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })` 应返回 `[{ first: "Tybalt", last: "Capulet" }]`。 ```js assert.deepEqual( whatIsInAName( [ { first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' } ], { last: 'Capulet' } ), [{ first: 'Tybalt', last: 'Capulet' }] ); ``` `whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })` 应返回 `[{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]`。 ```js assert.deepEqual( whatIsInAName([{ apple: 1 }, { apple: 1 }, { apple: 1, bat: 2 }], { apple: 1 }), [{ apple: 1 }, { apple: 1 }, { apple: 1, bat: 2 }] ); ``` `whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })` 应返回 `[{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }]`。 ```js assert.deepEqual( whatIsInAName( [{ apple: 1, bat: 2 }, { bat: 2 }, { apple: 1, bat: 2, cookie: 2 }], { apple: 1, bat: 2 } ), [ { apple: 1, bat: 2 }, { apple: 1, bat: 2, cookie: 2 } ] ); ``` `whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 })` 应返回 `[{ "apple": 1, "bat": 2, "cookie": 2 }]`。 ```js assert.deepEqual( whatIsInAName( [{ apple: 1, bat: 2 }, { apple: 1 }, { apple: 1, bat: 2, cookie: 2 }], { apple: 1, cookie: 2 } ), [{ apple: 1, bat: 2, cookie: 2 }] ); ``` `whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }], { "apple": 1, "bat": 2 })` 应返回 `[{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }]`。 ```js assert.deepEqual( whatIsInAName( [ { apple: 1, bat: 2 }, { apple: 1 }, { apple: 1, bat: 2, cookie: 2 }, { bat: 2 } ], { apple: 1, bat: 2 } ), [ { apple: 1, bat: 2 }, { apple: 1, bat: 2, cookie: 2 } ] ); ``` `whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3})` 应返回 `[]`。 ```js assert.deepEqual( whatIsInAName([{ a: 1, b: 2, c: 3 }], { a: 1, b: 9999, c: 3 }), [] ); ``` # --seed-- ## --seed-contents-- ```js function whatIsInAName(collection, source) { const arr = []; // Only change code below this line // Only change code above this line return arr; } whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }); ``` # --solutions-- ```js function whatIsInAName(collection, source) { const arr = []; const keys = Object.keys(source); collection.forEach(function(e) { if(keys.every(function(key) {return e[key] === source[key];})) { arr.push(e); } }); return arr; } ```