1

What is the correct way to get all objectkeys with a false value in an array like this in Javascript:

[
  { unknownkey1 : false },
  { unknownkey2 : true  },  
  { unknownkey3 : false },
  { unknownkey4 : true  },
  { unknownkey5 : false },
  { unknownkey6 : true  }
]

Result should be an array containg all keys with a false value.

What I want is a cleaner solution for this:

for(var i = 0; i < results.length; i++ ){

    for ( key in results[i] ) {

      if ( results[i].hasOwnProperty( key ) && results[i][key] === false ){

         console.log( key );

      }

    }

}

If the value is not false it contains another object. But they are not needed and I would prefere a way that ignores the child objects if possible.

user2429266
  • 390
  • 1
  • 3
  • 19
  • no. If not false it contains another object. But they are not needed and I would prefere a way that ignores the child objects if possible. – user2429266 Apr 17 '14 at 16:27

2 Answers2

3

You want to iterate through the array, and then iterate through each key in each of those objects, then store those false keys in another array. Pretty straightforward.

var data = [
  { unknownkey1 : false },
  { unknownkey2 : true  },  
  { unknownkey3 : false },
  { unknownkey4 : true  },
  { unknownkey5 : false },
  { unknownkey6 : true  }
];

var keys = [];

data.forEach(function(object) {
    for (var key in object) {
        if (object[key] === false) keys.push(key);
    }
});
Greg
  • 2,163
  • 1
  • 21
  • 23
  • Is this the only way? A loop within a loop which loops over each unwanted child objects too? I edited the question. If the value is not false it is another object. – user2429266 Apr 17 '14 at 16:29
  • @user2429266 can obviously do without the inner loop if you're never going to have more than one key in an object. But if that's the case, why are you using an array at all? – Greg Apr 17 '14 at 19:15
  • I don't see a better way to handle my situation in a better way. At first i try to get it to work.. Maybe after that, as soon as I understand the whole process I'm trying to figure out the array concept becomes deprecated. It is tricky for me. Doing all that async stuff and keep track of where false values occure and update them after the async stuff is done with data from a src that does not send something to identify the key. All I get is data that is in the order i requested them. embed.ly. That makes it hard for me, because I haven't really had deeper contact to all this async functions. – user2429266 Apr 17 '14 at 19:28
  • If I take a closer look... yes. the array concept is maybe not needed.. But Thats something to figure out after it works.^^ – user2429266 Apr 17 '14 at 19:34
  • No worries, there's always more time to cleanup/refactor things later ;) – Greg Apr 17 '14 at 19:36
1
var arr=[
  { unknownkey1 : false },
  { unknownkey2 : true  },  
  { unknownkey3 : false },
  { unknownkey4 : true  },
  { unknownkey5 : false },
  { unknownkey6 : true  }
]

// You can return a key for false values and nothing for true, 
// then filter out the empty indexes:

var falsies= arr.map(function(itm){
    return itm[Object.keys(itm)[0]]==false? Object.keys(itm)[0]:''}).
    filter(Boolean);

/*  returned value: (Array)
unknownkey1,unknownkey3,unknownkey5
*/
kennebec
  • 102,654
  • 32
  • 106
  • 127