kagorastidis
6/8/2016 - 7:58 AM

Remove object from array with key value. Needs Underscore.js

Remove object from array with key value. Needs Underscore.js

// example array of objects.
var arr = [{'Cow' : 'Moo'},{'Cat' : 'Meow'},{'Dog' : 'Bark'}];

var removeObjectFromArray = function(array, value){
  for(var i = 0; i<array.length; i++){
    if(_.invert(array[i])[value]){
      var index = _.invert(array[i])[value];
      }
	}
	array = _.reject(array, function(key){ return key[index] === value; });
	return array;
}

// example call
removeObjectFromArray(arr, 'Moo')