Need help with Wherefore art thou

Hello Everyone, I’m facing some issue with my Code. Please help me with this.

Challenge:

Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching property and value pairs (second argument). Each property and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.

For example, if the first argument is [{ first: “Romeo”, last: “Montague” }, { first: “Mercutio”, last: null }, { first: “Tybalt”, last: “Capulet” }], and the second argument is { last: “Capulet” }, then you must return the third object from the array (the first argument), because it contains the property and its value, that was passed on as the second argument.

My Solution:

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

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

I know may be this far my solution is not good too. So I request you to help me with the same.
Thanks in advance.

What is the problem that you are having - it seems to work. There is more than one possible solution, but this seems to work.

You’ll get better responses if you’re very specific about what you want.

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  var keys = Object.keys(source);
  arr = collection.filter(function(obj) {
    return keys.every(function(key) {
      return obj.hasOwnProperty(key) && obj[key] === source[key];
    });
  });
  
  // Only change code above this line
  return arr;
}

i like yours tho

Here’s mine.


function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line

  // notes... 
  // source.last == collection[2].last  ; to compare last name to collection object
  // output.push(collection[2])        ; to add collection object to output 
  // collection[0].hasOwnProperty(Object.keys(source)[0])     ; check if first object in collection has 
  //   exist same key of source
  
  // NOTE: the .map processes each item in object, and ALWAYS return a value.
  // The .filter only returns the original object that meets the criteria
  
  srcKeys = Object.keys(source);
    
  for (i=0; i < srcKeys.length ; i++ ){
    arr = collection.filter( 
      function(val){
        // console.log(val);
        if ((val.hasOwnProperty(srcKeys[i]) === true) && (val[srcKeys[i]] === source[srcKeys[i]] )) { 
          return true;
        } 
        return false;
      });
  
  } // for loop 
 
  return arr;
}

Thank you for your reply, here is the problem I’m facing.

Thanks! your code is working fine, but my facing errors in last two solutions. Trying to understand your code.

Thank you for your reply, Your code is also working but it’s showing a warning,

“Don’t make functions within loops.”

I would reccomend writing your own code.

Your code didn’t work for the final tests because you failed to account for the possibility that there was more than one source. You loop through the collection but then within that you need to loop through the source to check each item.

With regards to “Don’t make functions within loops.” that is a warning, not an error. I think it doesn’t like it because it creates the possibility of scoping errors but mainly I think because there is bandwidth costs to creating functions, and if you put that inside a loop, you create the possibility of massive performance issues. Not in this case, but it is possible.

I use the beta version so there may be some issue with testing being different. also agree with writing your own, “breaking shit” as i like to say, and going through the console to learn.