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.

4 Answers 4

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;
  }, {});
});
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Ah I see, try it with _.reduce instead of _.filter then. See my edit.
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: stackoverflow.com/questions/5080028/… I still have a lot to learn about functional coding
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);

Comments

1

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

objs = _.map(objs, function(x) {
    return _.omit(x, _.isUndefined)
})

Comments

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];
  }
}

3 Comments

When looping objects you should generally use a hasOwnProperty check, but I suppose that's really up to the OP.
Thanks fellas! I'm trying your solutions now. Will follow up shortly
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.