0

i have bind object 'p' with input fields using angularJS like this

<input ng-model="p.p1" type="text" />
<input ng-model="p.p2" type="text" />
<input ng-model="p.p3" type="text" />

it seems like my object is like this when these inputs are empty:

var p =
  {
    "p1": null,
    "p2": null,
    "p3": null
  };

I have tried

 Object.keys(p); // return [] (empty array)

Can any one tell me how I can get list of all keys contains in JavaScript object, including keys which contain null value as well ?

Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33
  • In which browser did you test it? – mohamedrias Feb 27 '15 at 07:04
  • i have tested it in firefox – Muhammad Suleman Feb 27 '15 at 07:05
  • I've tried it in chrome and safari and it works fine.. it returns the list of all keys even if the value is null – mohamedrias Feb 27 '15 at 07:05
  • version of firefox?? because it Object.keys is not implemented in lower version – Raja Sekar Feb 27 '15 at 07:07
  • @sallu It is working in Firefox too. I tried it on Firefox Dev v37. – Rahul Desai Feb 27 '15 at 07:08
  • @Raj : firefox version is 36.0 – Muhammad Suleman Feb 27 '15 at 07:08
  • possible duplicate of [Getting JavaScript object key list](http://stackoverflow.com/questions/3068534/getting-javascript-object-key-list) – Banana Feb 27 '15 at 07:11
  • no its not duplicate of that question @Banana because i am specifically asking about null value keys – Muhammad Suleman Feb 27 '15 at 07:13
  • @sallu it is still a duplicate because it works just the same: http://jsfiddle.net/u32ogcqa/ – Banana Feb 27 '15 at 07:13
  • but i am not getting null value keys that why i have asked this question @Banana – Muhammad Suleman Feb 27 '15 at 07:15
  • @Banana, the answer from that question does not work for him, so he needs a different answer. – Alexandru Severin Feb 27 '15 at 07:16
  • @sallu It works for me in all browsers, could be something something more to the code that is removing the elements? Try jsfiddle from Banana and tell us if that works for you or the result is the same. – Alexandru Severin Feb 27 '15 at 07:17
  • @AlexandruSeverin op uses firefox 36. i have tested it on firefox 36 and that answer works just fine. your comment above is most likely correct, he probably has another script block that alters the array – Banana Feb 27 '15 at 07:17
  • Yet from him is not working, I suspect he has some additional code that removes the values from the object before Object.keys() is called. – Alexandru Severin Feb 27 '15 at 07:20
  • @Alexandru Severin: i am using this in angularJS so does anything is there in angularJS which not let this happen ? i have bind object 'p' with form attributes – Muhammad Suleman Feb 27 '15 at 07:20
  • @sallu please post all of your code, angular on its own will not cause that issue. – Banana Feb 27 '15 at 07:21
  • @sallu it is possible that the script block where you initialize `p` fails and the var `p` remains undefined. also, i hate to ask but are you sure that the empty array that you see is actually the result of the key retrieval from `p` and is not returned by a different part of your code? – Banana Feb 27 '15 at 07:26
  • Ahhh.... i got it, actually problem is because of angularJS input field bindings, its not put any key+value in object if we do not fill input field. thats why its even not put key with NULL value in object. – Muhammad Suleman Feb 27 '15 at 07:30
  • Thank you ALL. i got my issue while discussing it. :) – Muhammad Suleman Feb 27 '15 at 07:31
  • @sallu good to hear that you resolved it. you should post it as an answer and accept it, it might help others who face similar issue in the future. – Banana Feb 27 '15 at 07:32

4 Answers4

0

Iterate through for loop and get those keys which have null.

for(var key in p) {
    if(p[key] == null) {
       // do your stuff here!
    }
}
Shreejibawa
  • 1,860
  • 1
  • 25
  • 35
  • its not loop through it and not reaching the 'if(p[key] == null)'. i guess its because length of 'p' id '0' so loop is not running. – Muhammad Suleman Feb 27 '15 at 07:10
0

why not use p.p1 ? or else you can always use p['p1']? if you don't define the key you want to access its free for the garbage collector to clean up.

You need to place a hook in your code that accesses that key directly, otherwise it will get cleaned up.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • the reason not using p.p1 is because i have a lot of fields in object and i have to put conditions on each attribute so i wanna loop though it – Muhammad Suleman Feb 27 '15 at 07:07
  • initialise it with something else than null. use something equal like -1 or "". null is basically the flag to the GC you want it cleaned up and you're done with it. – Tschallacka Feb 27 '15 at 07:08
0

Please try the below one. Object.keys in not supported in major browsers, So have a polyfill for that.

var p =
  {
    "p1": null,
    "p2": null,
    "p3": null
  },key;

function getObjectKeys(p){

  if(Object.keys){
    key = Object.keys(p);

    return key;
  }else{
    key = [];
    for(var k in p){ 
      key.push(k);
    }
    return key;
  }
}

getObjectKeys(p);

Live output

Raja Sekar
  • 2,062
  • 16
  • 23
0

Thank you all for your response, Actually the problem is not in JS code rather problem is with angularJS binding. its not put any key+value in object when input field is empty. so that's why i got object like this

 var p = {}; // $scope.p is looks same as this

so i did not got any key from

Object.keys(p); // is also working fine.

other solutions posted by other guys are also work fine.

Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33