简体   繁体   English

从javascript中的对象数组获取对象名称

[英]Get object names from a array of objects in javascript

i cannot find a solution of my issue. 我找不到我的问题的解决方案。 i probably making things more complicated. 我可能会使事情变得更复杂。

here is the array: 这是数组:

var Statistics = {
"STAT 1 COLUMNS" : ["Date","Group Name","Product Name","Version","Version Date","Status","Total","Total L0","Total L1","Total L2","To be validated","Created","Updated"],
"STAT 2 TITLE" : ["12/12/2013","Led Zeppelin","roofies","V5.3","2013-08-13 ","ACCEPTED <br/>(2013-9-10)","774","334","3","437","20","57","102"],
"STAT 3 TITLE" : ["22/11/2014","Deep Purple","upper","V1","2006-01-01 "," ","3","1","0","2","1"," "," "],
...
}

i would like to get the object name (such as 'STAT 1 COLUMNS', 'STAT 2 TITLE', 'STAT 3 TITLE'..) for a specific object, by specifing the index. 我想通过指定索引来获取特定对象的对象名称(例如“ STAT 1列”,“ STAT 2标题”,“ STAT 3标题” ..)。

for instance, givemepleasethenameoftheobject('2') -> "STAT 3 TITLE" 例如,请给我对象的名称('2')->“ STAT 3 TITLE”

can you please give me a hand? 你能帮我一下吗?

Try with: 尝试:

function givemepleasethenameoftheobject(index, data) {
  var i = 0;
  for ( var k in data ) {
    if ( index == i++ ) {
      return k;
    }
  }
}

var result = givemepleasethenameoftheobject(2, Statistics);

This should achieve what you need: 这应该可以实现您所需要的:

function getStatisticName(index)
{
    var count = 0;
    for(var i in Statistics)
    {
        if(count == index)
            return i;

        count++;
    }
}

See this jsFiddle 看到这个jsFiddle

Try the 'Object.keys();' 尝试使用“ Object.keys();” function. 功能。

Object.keys(Statistics); // ['STAT 1 COLUMNS', 'STAT 2 TITLE', 'STAT 3 TITLE'];

Object keys order is not guaranteed in JavaScript, so gimmeTheKey(Statistics, 2) is not guaranteed to return "STAT 3 TITLE" . 对象键顺序没有JavaScript的保证,所以gimmeTheKey(Statistics, 2) 不能保证返回"STAT 3 TITLE"

However: 然而:

function gimmeTheKey( obj, idx ){
    return Object.keys( obj )[idx];
}

You can loop through the members of an object like this: 您可以像这样遍历对象的成员:

for (var key in Statistics) {
  console.log(key);
}

However, you can not reliably access them by index like that. 但是,您不能像这样通过索引可靠地访问它们。 The members in an object is not guaranteed to remain in the same order that you create them, and different browsers actually do order the members differently. 不能保证对象中的成员保持与创建它们时相同的顺序,并且实际上不同的浏览器的成员顺序也不同。

If you want to access the member names by index in a predictable way, you would need to get all names and then sort them: 如果要以可预测的方式按索引访问成员名称,则需要获取所有名称,然后对它们进行排序:

function givemepleasethenameoftheobject(index) {
  var keys = [];
  for (var key in Statistics) {
    keys.push(key);
  }
  keys.sort();
  return keys[index];
}

If you don't need to be compatible with older browsers (eg Internet Explorer 8), you can use the Object.Keys method : 如果不需要与旧版浏览器(例如Internet Explorer 8)兼容,则可以使用Object.Keys方法

function givemepleasethenameoftheobject(index) {
  var keys = Object.keys(Statistics);
  keys.sort();
  return keys[index];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM