Tell us what’s happening:
I used spread operator on my code so If second arg has only values first arg already has, then the merged object should be the same as first arg. And Second arg will overwrite first arg properties of same name.
But I have an issue. When I run my code, I see in the output:
Hey @abdulrahman.mhd.anas, you sure you want to use filter as so?
Remember that filter keeps “the original” value if passes the given callback in a new array.
It’ s the map method that create a new array with the value updated.
Stop being “clever” and try to use every “fancy” method you can think of, I fear you got yourself in a slump trying to use spread operator at all cost.
In general try to solve the challenge firsts, even with a “brute” non-elegant solution, then refactor accordingly
For an head start:
collection.filter(function (item) {
// we know we have to return true or false to keep the element
// does item have all the keys in source?
// does item[key] value === source[key] value?
// if so keep it.
}
Do you know that you can take all keys of an Object into an array using Object.keys(obj)? Thant can come in handy.
Or you can loop an object using a for in loop.
I still have a small issue
When I run my code I see array of value. not the name of it. The challenge want me to return both the name and it’s value. My Code So Far :
function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
const keys2 = Object.keys(source);
for (var i = 0; i < collection.length; i++) {
var obj = collection[i];
const keys1 = Object.keys(obj);
// ...
if (obj.hasOwnProperty(keys2)) {
for (let key of keys1) {
if (obj[key] === source[key]) {
arr.push(source[key]);
}
}
}
}
// Only change code above this line
return arr;
}
console.log(
whatIsInAName(
[
{ first: "Romeo", last: "Montague" },
{ first: "Mercutio", last: null },
{ first: "Tybalt", last: "Capulet" }
],
{ last: "Capulet" }
)
);
I see on the output: [ 'Capulet' ]
I want helps and tips from you developers
Thanks All!