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】的全部内容了,欢迎留言评论进行交流!