Wherefore art thou

Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.

For example, if the first argument is [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], and the second argument is { last: "Capulet" }, then you must return the third object from the array (the first argument), because it contains the name and its value, that was passed on as the second argument.

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }) should return [{ first: "Tybalt", last: "Capulet" }].

Passed

whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 }) should return [{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }].

Passed

whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }) should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }].

Passed

whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 }) should return [{ "apple": 1, "bat": 2, "cookie": 2 }].

Passed

whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }], { "apple": 1, "bat": 2 }) should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }].

Passed

whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3}) should return []

  1. function whatIsInAName(collection, source) {
  2. var arr = [];
  3. // Only change code below this line
  4. var keys = Object.keys(source)
  5. arr = collection.filter(x => keys.every(k => {
  6. //这里应该用
  7. return obj.hasOwnProperty(k) && x[k] === source[k]
  8. //不应该用
  9. return k in x && x[k] === source[k]
  10. }
  11. ))
  12. // Only change code above this line
  13. return arr;
  14. }
  15. console.log(
  16. whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })
  17. )

答案

https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092

  1. function whatIsInAName(collection, source) {
  2. // "What's in a name? that which we call a rose
  3. // By any other name would smell as sweet.”
  4. // -- by William Shakespeare, Romeo and Juliet
  5. var srcKeys = Object.keys(source);
  6. // filter the collection
  7. return collection.filter(function(obj) {
  8. return srcKeys
  9. .map(function(key) {
  10. return obj.hasOwnProperty(key) && obj[key] === source[key];
  11. })
  12. .reduce(function(a, b) {
  13. return a && b;
  14. });
  15. });
  16. }
  17. // test here
  18. whatIsInAName(
  19. [
  20. { first: "Romeo", last: "Montague" },
  21. { first: "Mercutio", last: null },
  22. { first: "Tybalt", last: "Capulet" }
  23. ],
  24. { last: "Capulet" }
  25. );

Code Explanation

We start by filtering through collection using Array.filter().

Next, we map through all keys and return Boolean values based on the check conditions: both the key and its corresponding value must exist within the object we are filtering through.

Then we reduce the mapped Boolean values to a single Boolean that indicates whether all srcKeys pass the conditions checked above.

This single Boolean will be used to filter through the collection.

Relevant Links

Array.prototype.filter()

Array.prototype.reduce()

Object.hasOwnProperty()