简体   繁体   中英

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

Example:

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

How do I get the first '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.

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. Arrays are zero-indexed.

Have a look at this mdn page about arrays to learn more.


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)

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.


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 (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]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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