博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Javascript中访问对象的第一个属性?
阅读量:2288 次
发布时间:2019-05-09

本文共 2794 字,大约阅读时间需要 9 分钟。

本文翻译自:

Is there an elegant way to access the first property of an object... 是否有一种优雅的方式来访问对象的第一个属性...

  1. where you don't know the name of your properties 你不知道你的房产名称的地方
  2. 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楼

参考:


#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 for details on this. 根据ECMAScript不保证Object.keys返回顺序,但是非正式地是所有主要浏览器实现,请阅读以获取详细信息。


#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: 更多信息: :

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 library: 使用库的解决方案:

_.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实现。

转载地址:http://aicnb.baihongyu.com/

你可能感兴趣的文章
安装星际译王
查看>>
超强android根文件系统目录分析
查看>>
od命令的使用
查看>>
linux less命令(man默认的阅读工具)
查看>>
Linux Embedded System
查看>>
linux touch 命令详解
查看>>
restrict
查看>>
ubuntu配置TFTP
查看>>
解决Ubuntu的乱码问题
查看>>
Ubuntu中文乱码问题
查看>>
cscope和Taglist安装
查看>>
标记化结构初始化语法
查看>>
浅析Linux下core文件
查看>>
sizeof:(含位域)结构体内存对齐,压缩存储
查看>>
小企鹅输入法快捷键
查看>>
VC++深入详解之C++笔记
查看>>
Linux下netstat-nslookup-finger-ping-在线帮助-whereis
查看>>
ubuntu终端查看最近输入的命令
查看>>
在ubuntuserver上搭建tftp服务器
查看>>
如何配置带SourceCRT的开发环境
查看>>