简体   繁体   English

Javascript-如何获取数组中的第一个元素?

[英]Javascript - How do I get the first element in an array?

Example: 例:

var myArray = ['e', {pluribus: 'unum'}];

How do I get the first 'e'? 我如何获得第一个“ e”?

My actual array looks like this: 我的实际数组如下所示:

({'U.S., MCC':{score:"88.88", url:"http://ati.publishwhatyoufund.org/donor/usmcc/"}, GAVI:{score:"87.26", url:"http://ati.publishwhatyoufund.org/donor/gavi/"}, 'UK, DFID':{score:"83.49", url:"http://ati.publishwhatyoufund.org/donor/ukdfid/"}, UNDP:{score:"83.38", url:"http://ati.publishwhatyoufund.org/donor/undp/"}, 'World Bank, IDA':{score:"73.81", url:"http://ati.publishwhatyoufund.org/donor/world-bank-ida/"}, 'Global Fund':{score:"70.65", url:"http://ati.publishwhatyoufund.org/donor/global-fund/"}}) 

I need to get the name. 我需要知道名字 ie 'US, MCC' and then the score and the url - I am using this in highcharts as data points in a graph. 即“美国,我的客户中心”,然后是得分和网址-我在图表中将其用作图表中的数据点。

I know it should be simple, but I am a complete JS noob. 我知道它应该很简单,但是我是一个完整的JS新手。

Thanks 谢谢

To get the first element from your array, just use this: 要从数组中获取第一个元素,只需使用以下命令:

myArray[0]

Notice that 0 points at the first element in the array. 请注意, 0指向数组中的第一个元素。 Arrays are zero-indexed. 数组是零索引的。

Have a look at this mdn page about arrays to learn more. 看看这个关于数组的mdn页面,以了解更多信息。


However , what you have there isn't an array, it's an object. 但是 ,您所拥有的不是数组,而是一个对象。 You can't access those with numeric keys (like 0, 1, 2 etc) 您无法使用数字键(例如0, 1, 2等)访问它们

To get the first element of your object, you have to use the "key" to access that value. 要获取对象的第一个元素,您必须使用“键”来访问该值。
Assuming: 假设:

var myObject = {'U.S., MCC':{score:"88.88", url:"http://ati.publishwhatyoufund.org/donor/usmcc/"}, GAVI:{score:"87.26", url:"http://ati.publishwhatyoufund.org/donor/gavi/"}, 'UK, DFID':{score:"83.49", url:"http://ati.publishwhatyoufund.org/donor/ukdfid/"}, UNDP:{score:"83.38", url:"http://ati.publishwhatyoufund.org/donor/undp/"}, 'World Bank, IDA':{score:"73.81", url:"http://ati.publishwhatyoufund.org/donor/world-bank-ida/"}, 'Global Fund':{score:"70.65", url:"http://ati.publishwhatyoufund.org/donor/global-fund/"}}

Then: 然后:

myObject['U.S., MCC']

Will be: 将会:

{score:"88.88", url:"http://ati.publishwhatyoufund.org/donor/usmcc/"}

Or, as a more simple example: 或者,作为一个更简单的示例:

var foo = {
    'bar': 1,
    'wut': {'nested': 'you can nest objects! (and arrays, etc)'}
    baz: 'Objects, woo!',  // Quotes around keys aren't mandatory, unless you have
}                          // spaces in the keys: 'quotes mandatory'

foo['bar']     // 1
foo.wut.nested // 'you can nest objects! (and arrays, etc)'
foo.baz        // 'Objects, woo!' (you don't have to use the square brackets,
               //                  if the key is a simple string (No spaces))

Have a look at this mdn article about working with objects to learn more about those. 看一下这篇有关使用对象的mdn文章,以了解有关对象的更多信息。


Now, to actually get the "first" element in that object is tricky, since objects aren't sorted. 现在,由于未对对象进行排序,因此在该对象中实际获取“第一个”元素非常棘手。 (Even though they may appear so.) (即使它们看起来可能如此。)

You can loop through an object using a for...in , but there's no guarantee the items will show up in the same order, on different browsers: 您可以使用for...in遍历对象,但不能保证这些项目将以相同的顺序显示在不同的浏览器上:

for (var key in myObject) {
    if (myObject.hasOwnProperty(key)) { // Make sure it's a proper element on the object, not a prototype function.
        // key == ''U.S., MCC', for example,
        doSomethingWith(myObject[key]);
    }
}

You can iterate over objects in a sorted order, but there's some better answers out there about that. 您可以按排序的顺序遍历对象,但是对此还有一些更好的答案

Try to use this case: 尝试使用这种情况:

var myObj = {'U.S., MCC':{score:"88.88", url:"http://ati.publishwhatyoufund.org/donor/usmcc/"}, GAVI:{score:"87.26", url:"http://ati.publishwhatyoufund.org/donor/gavi/"}, 'UK, DFID':{score:"83.49", url:"http://ati.publishwhatyoufund.org/donor/ukdfid/"}, UNDP:{score:"83.38", url:"http://ati.publishwhatyoufund.org/donor/undp/"}, 'World Bank, IDA':{score:"73.81", url:"http://ati.publishwhatyoufund.org/donor/world-bank-ida/"}, 'Global Fund':{score:"70.65", url:"http://ati.publishwhatyoufund.org/donor/global-fund/"}};

for(v in myObj) {
    console.log("Obj key: "+v, myObj[v]);
}

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

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