whatIsInAName - solution needed for multiple key values passed for source parameter

whatIsInAName - solution needed for multiple key values passed for source parameter
0.0 0

#1

Below is working for single key value pair passed as argument for “source” parameter. but not able to figure for multiple key value pairs.

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  
  //   for(var j in source){
  //     var src = source[j];
  
  for(var i=0;i<collection.length;i++){
        var obj = collection[i];        
    for(var j in source){
       if(obj[j] == (source[j])){
          arr.push(obj);
        }  
    }         
  }
  
  // Only change code above this line
  return arr;
}

whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }],{ "a": 1, "b": 2});

#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

I’m not sure I follow what you’re trying to solve but I’m curious about this second loop.

What is the need to loop through the source?

If obj is an object and you’re trying to see if a key in the obj is also in the source, what do you think about looping through the obj instead?

for(var i=0; i < collection.length; i++){
        var obj = collection[i];     
    for(key in obj) {
        .......
        Look for key in source
   }   
  
  }

#4

Hi I am trying to find if all the key values from source are present in collections. The object that matches need to be pushed into the array.