Thursday, November 7, 2019

Filter out all of the matches on a single property within the objects in an object array based upon a second comparable array of blacklisted items in JavaScript.

var good = [];
var bad = [{id:27,alt:'h'},{id:69,alt:'i'},{id:42,alt:'j'},{id:23,alt:'k'}];
var ugly = [{id:69,alt:'l'},{id:13,alt:'m'},{id:86,alt:'n'},{id:42,alt:'o'},{id:23,alt:'p'}];
bad = bad.sort((yin, yang) => {
   if (yin.id > yang.id) {
      return 1;
   }
   if (yin.id < yang.id) {
      return -1;
   }
   return 0;
});
ugly = ugly.sort((yin, yang) => {
   if (yin.id > yang.id) {
      return 1;
   }
   if (yin.id < yang.id) {
      return -1;
   }
   return 0;
});
var spot = 0;
ugly.forEach(function(value) {
   if(value.id != bad[spot].id) {
      if (value.id > bad[spot].id) {
         while (value.id > bad[spot].id && spot < bad.length-1) {
            spot++;
         }
      }
      if (value.id != bad[spot].id) {
         good.push(value);
      }
   }
});
console.log(good);

 
 

Supposedly _.merge is the way to do this stuff with Lodash.

No comments:

Post a Comment