0

I have a big array of objects and i need to get the objects that have the propertie def set. No mather the value... Thanks in advance.

Leonel Matias Domingos
  • 1,922
  • 5
  • 29
  • 53

2 Answers2

5

You can use hasOwnProperty to check if a property is present, and Array.prototype.filter to filter only those items.

objArray = [ { def: 1, bar: 2}, { foo: 3, bar: 4}, { def: 5, bar: 6} ];

var result = objArray.filter(item => item.hasOwnProperty('def'));
console.log(result);

for es5 compatibility

objArray = [{
  def: 1,
  bar: 2
}, {
  foo: 3,
  bar: 4
}, {
  def: 5,
  bar: 6
}];

var result = objArray.filter(function(item) {
  return item.hasOwnProperty('def')
});
console.log(result);
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
0

There is not such function in lodash although you can try following code

`let aFilteredArray = [];
    _.forEach(YourArray,function(oElement){
      if(!_.isEmpty(oElement.def)){
        aFilteredArray.push(oElement);
      }
    };
RohanS404
  • 41
  • 1
  • 8