Intermediate Algorithm Scripting: Wherefore art thou (Prop Replacement)

Tell us what’s happening:
Hey guys,

I am trying to figure out how to replace “last” after “source.last” below with whatever the key value of “source” is but I am not sure how to do it. I also want to replace ‘Capulet’ with whatever that value is for “source.last” also. Does that make sense?

Can someone help me out here! Much appreciated!

Your code so far


function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line

var key = Object.keys(source).toString();
var newArr = collection
.filter(function(source) {
  return source.last == 'Capulet';
})
// Only change code above this line
return newArr;
}

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 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:

key is an array. If it has only one element this works as you intend, but what if it has more than one element? look at the tests, this is a thing you need to consider.

you also can’t use last in your code, as your code needs to be reusable for objects that don’t have that property.

the property keys you use should come from the keys array, shouldn’t they? try remember how to access object properties with variables.