Bonfire: Where art thou

Bonfire: Where art thou_第1张图片

function where(collection, source) {
    var arr = [];
    // What's in a name?
   arr =   collection.filter(function(x){
       var tmpAll = true;
       for(var prop in source)
       {
           if(x.hasOwnProperty(prop) && source.hasOwnProperty(prop) )
           {
               if(x[prop] != source[prop])
               {
                   tmpAll = false;
                   break;
               }
           }else
           {
               tmpAll = false;
               break;
           }
       }
       if(tmpAll)
       {
           console.log(x);
           return x;
       }
    });
    return arr;
}
where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

你可能感兴趣的:(FreeCodeCamp)