通过索引访问非数字对象属性? [英] Access non-numeric Object properties by index?
问题描述
如果我有一个这样的数组:
VAR ARR = [一,二,三化];
我可以做这个访问不同的部分:
的console.log(ARR [1]);
我如何才能通过他们的订单,而不是重点访问对象属性?
例如:
VAR OBJ = {
'东西':'真棒',
这一冲突:疯狂
},
JBO = {
这一冲突':'疯狂',
'东西':'真棒'
};
我将如何获得每个对象 - 东西第一个属性从 OBJ
和 JBO
-without明确使用属性名称?
现在,少数的你似乎认为像后我:
的console.log(OBJ ['东西']);
这是不是这样的,我专门找目标指数,就像第一个例子 - 如果可能的话
的我专门找目标指数,就像第一个例子 - 如果可能的话的
块引用>没有,这是不可能的。
您可以得到最接近的是获取对象的键数组,并使用:
VAR键= Object.keys(OBJ);
...但没有保证的钥匙将你定义的顺序返回。因此,它最终可能看起来像:
键[0]; // '更多'
键[1]; //'东西'If I have an array like this:
var arr = ['one','two','three'];
I can access different parts by doing this:
console.log(arr[1]);
How can I access object properties by their order rather than by key?
Example:
var obj = { 'something' : 'awesome', 'evenmore' : 'crazy' }, jbo = { 'evenmore' : 'crazy', 'something' : 'awesome' };
How would I get the first property for each object–"something" from
obj
and "evenmore" fromjbo
–without explicitly using the property name?Now, a few of you seem to think I'm after something like:
console.log(obj['something']);
This is not the case, I'm specifically looking to target the index, just like the first example - if it's possible.
解决方案"I'm specifically looking to target the index, just like the first example - if it's possible."
No, it isn't possible.
The closest you can get is to get an Array of the object's keys, and use that:
var keys = Object.keys( obj );
...but there's no guarantee that the keys will be returned in the order you defined. So it could end up looking like:
keys[ 0 ]; // 'evenmore' keys[ 1 ]; // 'something'
这篇关于通过索引访问非数字对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!