1.Javascript对象的长度不能用.length获取,用js原生的Object.keys可以获取到对象的长度

var obj = {'name' : 'Tom' , 'sex' : 'male' , 'age' : '14'}; 
var arr = Object.keys(obj); 
console.log(arr);  // ['name','sex','age'] 
console.log(arr.length);  //3 

2. javascript获取json对象的key名称的两种方法
方法1
json = { Key: 'richard', Value: '8' }
for (key in json){
    console.log(key);
}

结果
Key
Value

方法2
javascript中,Object具有一个key属性,可以返回json对象的key的数组
(Object has a property keys, returns an Array of keys from that Object)

用法:
Object.keys(json)
json = { Key: 'richard', Value: '8' }
console.log(Object.keys(json));
 
结果
[ 'Key', 'Value' ]


以上就是【js获取对象名称key和长度length】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)

与本文相关的软件

发表我的评论

最新评论

  1. 暂无评论