Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Why doesn't this work?
  2.  
  3. function whatIsInAName(first, second) {
  4.   var srcKeys = Object.keys(second);
  5.  
  6.   return (first.filter(function(obj) {
  7.     for (var i = 0; i < srcKeys.length; i++) {
  8.       return (!obj.hasOwnProperty(srcKeys[i]) ||
  9.         obj[srcKeys[i]] !== second[srcKeys[i]] ? false : true);
  10.     }
  11.   }));
  12. }
  13. console.log(whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3}));
  14.  
  15. /// --------------------- Why doesn't the above work, but the below does?
  16.  
  17. // This works
  18. function whatIsInAName(collection, source) {
  19.   var srcKeys = Object.keys(source);
  20.  
  21.   return collection.filter(function(obj) {
  22.     for (var i = 0; i < srcKeys.length; i++) {
  23.       if (!obj.hasOwnProperty(srcKeys[i]) ||
  24.         obj[srcKeys[i]] !== source[srcKeys[i]]
  25.       ) {
  26.         return false;
  27.       }
  28.     }
  29.     return true;
  30.   });
  31. }
  32.  
  33. console.log(whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3}));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement