2

I am trying to do something in pure JS. I have an Object like so

var data = {
    "Something": true,
    "Something else": false,
    "Blah": false,
    "a fish ISA": true
};

What I want to do is filter anything which has true to its own Object. At the moment I am trying

var thingFunctions = Object.keys(data).filter(function(e) {
    return typeof data[e] == 'function'
});

But I think this is incorrect. How can I get the data with a value of true to its own Object?

Thanks

3
  • typeof data[e] == 'function' has nothing to with true or false booleans - you should be looking at something like typeof data[e] == 'boolean' && !!(data[e]) Commented Feb 9, 2016 at 15:47
  • 1
    or just data[e] === true Commented Feb 9, 2016 at 15:48
  • 1
    @pandorym Indeed, just wanted to stay in the style provided :) Commented Feb 9, 2016 at 15:49

4 Answers 4

3

So firstly you have no functions inside data, so doing a check on functions will return nothing.

var data = {
    "Something": true,
    "Something else": false,
    "Blah": false,
    "a fish ISA": true
};

var thingFunctions = Object.keys( data ).filter(function( e ) {
    return data[ e ] === true;
});

console.log( thingFunctions );

// Will result in [ 'Something', 'a fish ISA' ]
Sign up to request clarification or add additional context in comments.

Comments

3

You were asking for an object.

I use Array.prototype.forEach() for the iteration over the keys and an empty object for the result filtered, because I can not filter objects:

var data = {
        "Something": true,
        "Something else": false,
        "Blah": false,
        "a fish ISA": true
    },
    filtered = {};

Object.keys(data).forEach(function(e) {
    if (data[e] === true) {                      // explicit testing for boolean and true!
        filtered[e] = data[e];
    }
});

document.write('<pre>' + JSON.stringify(filtered, 0, 4) + '</pre>');

Comments

3

As you want to only have all true value we can convert them to true false values by doing the following.

!!({}) // true
!!([]) // true
!!('') // false
!!(true) // true
!!(false) // false
...

With this we can see if any data type is true or false by doing the following.

var thingFunctions = Object.keys(data).filter(function(e) {
    return !!(data[e]);
});

6 Comments

This would return false when the value is true, is that correct?
But the OP wants an object where only the true objects remain, this would do the opposite. You would need a double operator: !!data[e].
In that case you case do it again !!() this will convert it to a true false value and not convert it.
No it will not. !!(false) === false and !!(true) === true - What you are doing is !(false) === true because you invert their boolean only once. Which means you are returning false for elements that have a value of true, which means they wont be included in the eventual output of the filter.
Is that better? I first thought he wanted the opposite values I apologize.
|
0

To get a new object instead of just an array of the keys that point to a value of true (not truthy such as 1 or "hello"):

var onlyTrues = {};

for (var prop in data) {
  if (data[prop] === true) {
    onlyTrues[prop] = data[prop];
  }
}

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.