131 lines
3.7 KiB
Markdown
131 lines
3.7 KiB
Markdown
![]() |
---
|
||
|
id: a8e512fbe388ac2f9198f0fa
|
||
|
title: Perché sei tu
|
||
|
challengeType: 5
|
||
|
forumTopicId: 16092
|
||
|
dashedName: wherefore-art-thou
|
||
|
---
|
||
|
|
||
|
# --description--
|
||
|
|
||
|
Crea una funzione che scorra un array di oggetti (primo argomento) e restituisca un array di tutti gli oggetti che hanno coppie di nome e valore corrispondenti (secondo argomento). In caso di corrispondenza, dovranno essere incluse tutte le coppie nome / valore dell'oggetto source.
|
||
|
|
||
|
Ad esempio, se il primo argomento è `[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }]`, e il secondo argomento `{ last: "Capulet" }`, dovrai restituire il terzo oggetto dall'array (il primo argomento), perché contiene il nome e il suo valore, che sono stati passati come secondo argomento.
|
||
|
|
||
|
# --hints--
|
||
|
|
||
|
`whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })` dovrebbe restituire `[{ 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 })` dovrebbe restituire `[{ "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 })` dovrebbe restituire `[{ "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 })` dovrebbe restituire `[{ "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 })` dovrebbe restituire `[{ "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})` dovrebbe restituire `[]`
|
||
|
|
||
|
```js
|
||
|
assert.deepEqual(
|
||
|
whatIsInAName([{ a: 1, b: 2, c: 3 }], { a: 1, b: 9999, c: 3 }),
|
||
|
[]
|
||
|
);
|
||
|
```
|
||
|
|
||
|
# --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" });
|
||
|
```
|
||
|
|
||
|
# --solutions--
|
||
|
|
||
|
```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;
|
||
|
}
|
||
|
```
|