简体   繁体   English

获取包含NULL值的JavaScript / angularJS对象键

[英]Get JavaScript/angularJS object keys which contains NULL value

i have bind object 'p' with input fields using angularJS like this 我使用angularJS将对象'p'绑定到输入字段,就像这样

<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 ? 任何人都可以告诉我如何获取JavaScript对象中包含的所有键的列表,包括包含空值的键?

Iterate through for loop and get those keys which have null. 迭代for循环并获取那些具有null的键。

for(var key in p) {
    if(p[key] == null) {
       // do your stuff here!
    }
}

why not use p.p1 ? 为什么不使用p.p1 or else you can always use p['p1'] ? 或者你总是可以使用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. 您需要在代码中放置一个直接访问该密钥的钩子,否则它将被清除。

Please try the below one. 请尝试下面的一个。 Object.keys in not supported in major browsers, So have a polyfill for that. 主要浏览器不支持Object.keys,因此有一个polyfill。

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 实时输出

Thank you all for your response, Actually the problem is not in JS code rather problem is with angularJS binding. 谢谢大家的回复,其实问题不在于JS代码而是问题在于angularJS绑定。 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. 其他人发布的其他解决方案也可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javascript获取属性包含数组的对象作为值 - get the object which property contains an array as the value using javascript 从包含特定字符串的对象中选择键作为Javascript中的子字符串 - Pick keys from object which contains specific string as substring in Javascript "Javascript获取包含值的键索引" - Javascript get Index of Key which contains value 如何检查javascript对象是否包含null值或它本身是否为null - how to check if a javascript object contains null value or it itself is null 获取Javascript中具有最高值的对象键 - Get object keys with the highest value in Javascript 如何为Javascript对象中的属性设置值,该属性由键数组标识 - How to set value to a property in a Javascript object, which is identified by an array of keys 确定 JavaScript object 是否包含数组中的所有键并且所有键都没有空值 - Determine if a JavaScript object contains all keys from array & none of the keys have empty value 如何获取包含在嵌套数组中给定值的对象中的项目 - How get items in object which contains in nested array given value 从包含特定值的数组中获取对象 - Get an object from array which contains a specific value 获取具有属性的数组中的对象,该属性是包含匹配值的数组 - Get an Object in an Array that has a property which is an Array that contains a matching value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM