本文翻译自:How to access the first property of an object in Javascript?
Is there an elegant way to access the first property of an object... 是否有一种优雅的方式来访问对象的第一个属性...
- where you don't know the name of your properties 你不知道你的房产名称的地方
- without using a loop like
for .. in
or jQuery's$.each
不使用像for .. in
或者jQuery的$.each
For example, I need to access foo1
object without knowing the name of foo1: 例如,我需要在不知道foo1名称的情况下访问foo1
对象:
var example = {
foo1: { /* stuff1 */},
foo2: { /* stuff2 */},
foo3: { /* stuff3 */}
};
#1楼
参考:https://stackoom.com/question/47n9/如何在Javascript中访问对象的第一个属性
#2楼
var obj = { first: 'someVal' };
obj[Object.keys(obj)[0]]; //returns 'someVal'
Using this you can access also other properties by indexes. 使用此方法,您还可以通过索引访问其他属性。 Be aware tho! 请注意! Object.keys
return order is not guaranteed as per ECMAScript however unofficially it is by all major browsers implementations, please read https://stackoverflow.com/a/23202095 for details on this. 根据ECMAScript不保证Object.keys
返回顺序,但是非正式地是所有主要浏览器实现,请阅读https://stackoverflow.com/a/23202095以获取详细信息。
#3楼
I don't recommend you to use Object.keys since its not supported in old IE versions. 我不建议您使用Object.keys,因为它在旧的IE版本中不受支持。 But if you really need that, you could use the code above to guarantee the back compatibility: 但是如果你真的需要它,你可以使用上面的代码来保证后面的兼容性:
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;
}})()};
Feature Firefox (Gecko)4 (2.0) Chrome 5 Internet Explorer 9 Opera 12 Safari 5 功能Firefox(Gecko)4(2.0)Chrome 5 Internet Explorer 9 Opera 12 Safari 5
More info: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys 更多信息: https : //developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys
But if you only need the first one, we could arrange a shorter solution like: 但如果你只需要第一个,我们可以安排一个更短的解决方案,如:
var data = {"key1":"123","key2":"456"};
var first = {};
for(key in data){
if(data.hasOwnProperty(key)){
first.key = key;
first.content = data[key];
break;
}
}
console.log(first); // {key:"key",content:"123"}
#4楼
一个规则版本:
var val = example[function() { for (var k in example) return k }()];
#5楼
你也可以做Object.values(example)[0]
。
#6楼
Solution with lodash library: 使用lodash库的解决方案:
_.find(example) // => {name: "foo1"}
but there is no guarantee of the object properties internal storage order because it depends on javascript VM implementation. 但是不能保证对象属性内部存储顺序,因为它依赖于javascript VM实现。