Javascript 按索引访问非数字对象属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7866275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:53:52  来源:igfitidea点击:

Access non-numeric Object properties by index?

javascriptarrays

提问by daryl

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 objand "evenmore" from jbo–without explicitly using the property name?

如何objjbo不明确使用属性名称的情况下获取每个对象的第一个属性——“某物”和“甚至更多” ?

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.

情况并非如此,我专门针对索引,就像第一个示例一样 - 如果可能的话。

回答by user113716

"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'

回答by 0x499602D2

The only way I can think of doing this is by creating a method that gives you the property using Object.keys();.

我能想到的唯一方法是创建一个方法,使用Object.keys();.

var obj = {
    dog: "woof",
    cat: "meow",
    key: function(n) {
        return this[Object.keys(this)[n]];
    }
};
obj.key(1); // "meow"

Demo: http://jsfiddle.net/UmkVn/

演示:http: //jsfiddle.net/UmkVn/

It would be possible to extend this to all objects using Object.prototype;but that isn't usually recommended.

可以将其扩展到所有使用的对象,Object.prototype;但通常不建议这样做。

Instead, use a function helper:

相反,使用函数助手:

var object = {
  key: function(n) {
    return this[ Object.keys(this)[n] ];
  }
};

function key(obj, idx) {
  return object.key.call(obj, idx);
}

key({ a: 6 }, 0); // 6

回答by Arthur Senna

You can use the Object.values()method if you dont want to use the Object.keys().

您可以使用该Object.values()方法,如果你不想使用Object.keys()

As opposed to the Object.keys()method that returns an array of a given object's own enumerable properties, so for instance:

Object.keys()返回给定对象自己的可枚举属性数组的方法相反,例如:

const object1 = {
 a: 'somestring',
 b: 42,
 c: false
};

console.log(Object.keys(object1));

Would print out the following array:

将打印出以下数组:

[ 'a', 'b', 'c' ]

The Object.values()method returns an array of a given object's own enumerable property values.

Object.values()方法返回给定对象自己的可枚举属性的数组values

So if you have the same object but use values instead,

因此,如果您有相同的对象但使用值,

const object1 = {
 a: 'somestring',
 b: 42,
 c: false
};

console.log(Object.values(object1));

You would get the following array:

你会得到以下数组:

[ 'somestring', 42, false ]

So if you wanted to access the object1.b, but using an index instead you could use:

因此,如果您想访问object1.b, 但使用索引,您可以使用:

Object.values(object1)[1] === 42

You can read more about this method here.

您可以在此处阅读有关此方法的更多信息。

回答by cuzzea

var obj = {
    'key1':'value',
    '2':'value',
    'key 1':'value'
}

console.log(obj.key1)
console.log(obj['key1'])
console.log(obj['2'])
console.log(obj['key 1'])

// will not work
console.log(obj.2)

Edit:

编辑:

"I'm specifically looking to target the index, just like the first example - if it's possible."

“我特别希望以索引为目标,就像第一个例子一样——如果可能的话。”

Actually the 'index' is the key. If you want to store the position of a key you need to create a custom object to handle this.

实际上,“索引”是关键。如果要存储键的位置,则需要创建一个自定义对象来处理此问题。

回答by Kareem

Get the array of keys, reverse it, then run your loop

获取键数组,反转它,然后运行你的循环

  var keys = Object.keys( obj ).reverse();
  for(var i = 0; i < keys.length; i++){
    var key = keys[i];
    var value = obj[key];
    //do stuff backwards
  }

回答by Meysam Khoshbakht

by jquery you can do this:

通过 jquery,你可以这样做:

var arr = $.map(obj,function(value, key) {
    return value;
});
alert(obj[0]);

回答by arfa

you can create an array that filled with your object fields and use an index on the array and access object properties via that

您可以创建一个填充了对象字段的数组,并在数组上使用索引并通过该索引访问对象属性

propertiesName:['pr1','pr2','pr3']

this.myObject[this.propertiesName[0]]

回答by Thiago Loddi

If you are not sure Object.keys() is going to return you the keys in the right order, you can try this logic instead

如果您不确定 Object.keys() 是否会以正确的顺序返回键,您可以试试这个逻辑

var keys = []
var obj = {
    'key1' : 'value1',
    'key2' : 'value2',
    'key3' : 'value3',
}
for (var key in obj){
    keys.push(key)
}
console.log(obj[keys[1]])
console.log(obj[keys[2]])
console.log(obj[keys[3]])