Can this solution be optimized in any way?

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
let sourceKeys=Object.keys(source)
//console.log(sourceKeys)
arr=collection.filter(item => 
{
  for(let prop of sourceKeys)
  {
    if(source[prop]!==item[prop])
    {
      return false;
    }
  }
  return true;
})

console.log(arr)
// Only change code above this line
return arr;
}

whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 });
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Challenge: Wherefore art thou

Link to the challenge:

I added spoiler tags to your code.

1 Like

a possible small improvement could be, to not save the source object keys in an array and loop thru that array, but loop directly thru the object keys. It saves a line of code, not sure how it would translate performance wise.

for (let key in source){
  // your condition
}
1 Like

thanks , i really don’t want to spoil the problem for those who will be trying hard

yeah , i added some useless code there

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.