2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: a8e512fbe388ac2f9198f0fa
|
2021-03-15 03:20:39 +00:00
|
|
|
title: 找出包含特定键值对的对象
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 5
|
2020-09-07 08:10:29 +00:00
|
|
|
forumTopicId: 16092
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: wherefore-art-thou
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
创建一个查看对象数组(第一个参数)的函数,并返回具有匹配的名称和值对的所有对象的数组(第二个参数)。 如果要包含在返回的数组中,则源对象的每个名称和值对都必须存在于集合中的对象中。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
比如,如果第一个参数是 `[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }]`,第二个参数是 `{ last: "Capulet" }`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })` 应返回 `[{ first: "Tybalt", last: "Capulet" }]`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
whatIsInAName(
|
|
|
|
[
|
|
|
|
{ first: 'Romeo', last: 'Montague' },
|
|
|
|
{ first: 'Mercutio', last: null },
|
|
|
|
{ first: 'Tybalt', last: 'Capulet' }
|
|
|
|
],
|
|
|
|
{ last: 'Capulet' }
|
|
|
|
),
|
|
|
|
[{ first: 'Tybalt', last: 'Capulet' }]
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })` 应返回 `[{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert.deepEqual(
|
|
|
|
whatIsInAName([{ apple: 1 }, { apple: 1 }, { apple: 1, bat: 2 }], {
|
|
|
|
apple: 1
|
|
|
|
}),
|
|
|
|
[{ apple: 1 }, { apple: 1 }, { apple: 1, bat: 2 }]
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`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 }]`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```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 }
|
|
|
|
]
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 })` 应返回 `[{ "apple": 1, "bat": 2, "cookie": 2 }]`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```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 }]
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`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 }]`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```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 }
|
|
|
|
]
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3})` 应返回 `[]`。
|
2020-09-07 08:10:29 +00:00
|
|
|
|
2018-10-10 22:03:03 +00:00
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
whatIsInAName([{ a: 1, b: 2, c: 3 }], { a: 1, b: 9999, c: 3 }),
|
|
|
|
[]
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function whatIsInAName(collection, source) {
|
|
|
|
var 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" });
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
function whatIsInAName(collection, source) {
|
|
|
|
var arr = [];
|
|
|
|
var keys = Object.keys(source);
|
|
|
|
collection.forEach(function(e) {
|
|
|
|
if(keys.every(function(key) {return e[key] === source[key];})) {
|
|
|
|
arr.push(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
```
|