1

I have the following array:

[{object}, {object}, {object}]

Each object looks something like:

{key0: 3, key1: undefined, key2: 7}

I want to filter the array for undefined properties so that each object in the array now looks like:

{key0: 3, key2: 7}

I've tried everything with Lo_Dash and I'm thinking I must be going crazy.

rashadb
  • 2,515
  • 4
  • 32
  • 57

4 Answers4

2

What code do you have written right now?

You could do something like this:

_.each(array, function(item, index, collection){
  collection[index] = _.filter(item, function(value){
    return value !== undefined;
  });
});

This will iterate over the array, then filter each object in the array.

EDIT:

If you'd like to maintain the array elements as objects, you can use _.reduce instead. IE:

_.each(array, function(item, index, collection){
  collection[index] = _.reduce(item, function(result, value, key){
    if(value !== undefined) { result[key] = value; }
    return result;
  }, {});
});
Danny Delott
  • 6,756
  • 3
  • 33
  • 57
  • I tried this. It definitely takes out the undefined value but it also reformats my array of objects to an array of arrays; I need to keep the key value pairs that have defined values as key value pairs. – rashadb May 26 '15 at 19:58
  • Ah I see, try it with `_.reduce` instead of `_.filter` then. See my edit. – Danny Delott May 26 '15 at 20:02
  • Great solution! Reduce is really helpful but underused for those of us not in the know . . . I came up with a solution for concating arrays with reduce fairly recently: http://stackoverflow.com/questions/5080028/what-is-the-most-efficient-way-to-concatenate-n-arrays-in-javascript/30421311#30421311 I still have a lot to learn about functional coding – rashadb May 26 '15 at 20:07
1

This is the approach that I would take. Use filter on your array and check for all the undefined props in a loop. Then simply delete them from your object. I just looked and another poster noted this. I've included the hasOwnProperty check as well.

var data = [{
    key0: 3,
    key1: undefined,
    key2: 7
}, {
    key0: 4,
    key1: undefined,
    key2: 8
}, {
    key0: 5,
    key1: undefined,
    key2: 9
}];

function removeUndefined(obj) {
    for (var prop in obj) {
        if (obj.hasOwnProperty(prop) && obj[prop] === undefined) {
            // do stuff
           delete obj[prop];
        }
    }
    return obj;
}

var filtered = data.filter(removeUndefined);

console.log(filtered);
gautsch
  • 774
  • 6
  • 11
1

In LoDash you can use a combination of map + omit:

objs = _.map(objs, function(x) {
    return _.omit(x, _.isUndefined)
})
georg
  • 211,518
  • 52
  • 313
  • 390
0

You can loop through all properties and remove undefined properties using the "delete" keyword.

var myObj = {key0: 3, key1: undefined, key2: 7};

for (var i in myObj) {
  if (myObj[i] === undefined) {
    delete myObj[i];
  }
}
KDB
  • 19
  • 2
  • When looping objects you should generally use a `hasOwnProperty` check, but I suppose that's really up to the OP. – bearfriend May 26 '15 at 19:53
  • Thanks fellas! I'm trying your solutions now. Will follow up shortly – rashadb May 26 '15 at 19:55
  • I a similar version to what you provided with LoDash but it seems like I can't use the delete key word while using strict mode. Any solution around the strict mode limitation besides turning it off? – rashadb May 26 '15 at 20:03