2

I am trying to remove the undefined values from this array: [undefined, "een", "twee", undefined]. The collection is an object like this:

{container:{id:1},container2:{id:2},container3:{pid:2}}

This does not work:

_.remove(_.map(collection,'id'),"undefined")

How can I do it?

3
  • 2
    undefined and "undefined" are not the same thing. _.remove(_.map(collection,'id'), undefined); works. Voting to close as typo. Commented Dec 15, 2016 at 1:37
  • 1
    Waarom lodash, array.filter werkt goed Commented Dec 15, 2016 at 1:42
  • .filter also removes other untruthy values like false and 0 Commented Dec 15, 2016 at 1:53

2 Answers 2

2

Try _.without(_.map(collection, 'id'), undefined).

_.remove() returns the removed items.

Sign up to request clarification or add additional context in comments.

Comments

0

For an array, you can use _.compact([NaN, 1, 0, 2, null, 5, undefined, 4]) to remove all falsey values, which will produce [1, 2, 5, 4]

_.compact docs

In case your collection is an object, you can use

_.pickBy({foo: 'bar', baz: undefined}, _.identity)

to achieve a similar result: {foo: 'bar'}.

Of course, you can always use any other function instead of _.identity if you have more complex logic.

I would also suggest looking at _.remove() depending on your needs.

Also, everything can be achieved with _.filter,_.map,_.reduce.

Good luck!

Comments

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.