假设我这样创建了一个对象:
var myObject =
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
如何最好地检索属性名称列表?也就是说,我想最终得到一些变量'keys',使其具有以下特点:
keys == ["ircEvent", "method", "regex"]
假设我这样创建了一个对象:
var myObject =
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
如何最好地检索属性名称列表?也就是说,我想最终得到一些变量'keys',使其具有以下特点:
keys == ["ircEvent", "method", "regex"]
如果有帮助的话,Mozilla在浏览器不支持的情况下提供了完整的实现细节:
if (!Object.keys) {
Object.keys = (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
var result = [];
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop)) result.push(prop);
}
if (hasDontEnumBug) {
for (var i=0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
}
}
return result;
};
})();
}
您可以按照自己的喜好将其包含在某种类型的 extensions.js
文件中,位于脚本堆栈的顶部。
在支持js 1.8的浏览器中:
[i for(i in obj)]
使用ES6及其之后的版本(ECMAScript 2015),您可以像这样获取所有属性:
let keys = Object.keys(myObject);
如果你想列出所有的值:
let values = Object.keys(myObject).map(key => myObject[key]);
let valudes = Object.values(myObject)
也能正常工作。 - Tomas基于已接受的答案,继续讲解。如果该对象具有你想要调用的属性,比如 .properties(),请尝试!
var keys = Object.keys(myJSONObject);
for (var j=0; j < keys.length; j++) {
Object[keys[j]].properties();
}
由于我在几乎每个项目中都使用underscore.js,因此我会使用keys
函数:
var obj = {name: 'gach', hello: 'world'};
console.log(_.keys(obj));
['name', 'hello']
mylittleJson = {
"one": "blah",
"two": {
"twoone": "",
"twotwo": "",
"twothree": ['blah', 'blah']
},
"three": ""
}
它将打印出以下内容:
.one
.two.twoone
.two.twotwo
.two.twothree
.three
这是一个函数
function listatts(parent, currentJson){
var attList = []
if (typeof currentJson !== 'object' || currentJson == undefined || currentJson.length > 0) {
return
}
for(var attributename in currentJson){
if (Object.prototype.hasOwnProperty.call(currentJson, attributename)) {
childAtts = listatts(parent + "." + attributename, currentJson[attributename])
if (childAtts != undefined && childAtts.length > 0)
attList = [...attList, ...childAtts]
else
attList.push(parent + "." + attributename)
}
}
return attList
}
mylittleJson = {
"one": "blah",
"two": {
"twoone": "",
"twotwo": "",
"twothree": ['blah', 'blah']
},
"three": ""
}
console.log(listatts("", mylittleJson));
希望这也能有所帮助。
IE不支持使用for(i in obj)来访问本地属性。以下是我能找到的所有属性列表。
看起来stackoverflow进行了一些愚蠢的过滤。
该列表可在此Google组帖子底部找到:- https://groups.google.com/group/hackvertor/browse_thread/thread/a9ba81ca642a63e0
这个解决方案适用于我的情况,并且跨浏览器。
var getKeys = function(obj) {
var type = typeof obj;
var isObjectType = type === 'function' || type === 'object' || !!obj;
// 1
if(isObjectType) {
return Object.keys(obj);
}
// 2
var keys = [];
for(var i in obj) {
if(obj.hasOwnProperty(i)) {
keys.push(i)
}
}
if(keys.length) {
return keys;
}
// 3 - bug for ie9 <
var hasEnumbug = !{toString: null}.propertyIsEnumerable('toString');
if(hasEnumbug) {
var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
var nonEnumIdx = nonEnumerableProps.length;
while (nonEnumIdx--) {
var prop = nonEnumerableProps[nonEnumIdx];
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
keys.push(prop);
}
}
}
return keys;
};
_.keys(myJSONObject)
- Endy TjahjonoObject.keys(obj)
。但有时您也需要获取不可枚举的属性。如果需要获取这些属性,请记住使用Object.getOwnPropertyNames(obj)
。参考来源:https://dev59.com/9nVC5IYBdhLWcg3wsTfv#32413145 - Andrew