Tell us what’s happening:
Your code so far
function whatIsInAName(collection, source) {
let src = Object.keys(source)
return collection.filter((el) => src.map((k) => el.hasOwnProperty(k) && el[k]=== source[k])).reduce((a, b) => a && b);
}
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36
.
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
JScars
July 27, 2019, 10:10pm
2
Your function only returns 1 element… You could try to be more clear writing your code; saving space is almost never useful… Your code must be readable when you are working with others.
1 Like
Hi, the reduce method should be concatenated after the map function instead of the filter one.
This is the working code:
let src = Object.keys(source);
arr = collection.filter(
(el) => {
return src.map((k) => el.hasOwnProperty(k) && el[k] === source[k]).reduce((a, b) => a && b);
});