1

I have the following object array:

 {
   running: false,
   swimming: false,
   drawing: true
  }

I want to filter the object array with only those that have false properties. From there I want to get only the keys of the array.

eg.

 [running, swimming]

I have the first part of removing values that have truth booleans through the following:

var filtered = _.pick(hobbies, function(value, key) {return !value;});

What would be the best way to "flatten" the current array so that I only have the key values?

2 Answers 2

4

The Lodash _.keys() function provides precisely what you are asking for:

var hobbies = {
    running: false,
    swimming: false,
    drawing: true
};

var filtered = _.keys(_.pick(hobbies, function(value) {return !value;}));

console.log(filtered);

// alternate approach using chain notation
var keys = _
    .chain(hobbies)
    .pick(v => !v)
    .keys();

console.log(keys);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.9.3/lodash.js"></script>

Note that in Lodash 4.x, _.pick() has changed and you should instead use _.pickBy() aboce.

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

Comments

3

As it is possible with pure JavaScript, then I suggesting you to do it using Object.keys and Array.prototype.filter

var obj = {
  running: false,
  swimming: false,
  drawing: true
};

var items = Object.keys(obj).filter(k => !obj[k]);
console.log(JSON.stringify(items));

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.