Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript -filtering object

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

like image 663
katie hudson Avatar asked Jul 12 '26 14:07

katie hudson


1 Answers

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' ]
like image 56
iSkore Avatar answered Jul 14 '26 02:07

iSkore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!