kyle_13
kyle_13

Reputation: 1233

How to access value of a key by index in an array in jQuery?

Take the example below, it's part of a json response. I want to access the "TestUser4" value of the fourth "ID". How can I achieve this with jQuery?

"UIDs": [
    {
      "ID": "TestUser1",
      "Type": "Ext"
    },
    {
      "ID": " TestUser2",
      "Type": "Int"
    },
    {
      "ID": "TestUser3",
      "Type": "Ext"
    },
    {
      "ID": "TestUser4",
      "Type": "Sys"
    }
  ]

My code is something similar to the following:

jQuery.ajax({
    type: "GET",
    dataType: "json",
    success: function( data ) {
        console.log( "ID: " + data.UIDs.ID[ 3 ].value );
    }
});

Upvotes: 0

Views: 51

Answers (2)

KpTheConstructor
KpTheConstructor

Reputation: 3291

You are pretty close.. What you want to do is access ID at the third index of your UIDs array like so :

data.UIDs[3].ID

Example Use :

jQuery.ajax({
    type: "GET",
    dataType: "json",
    success: function( data ) {

        console.log( "ID: " + data.UIDs[3].ID);
    }
});

Hope this helps..

Upvotes: 1

Johnathan Ralls
Johnathan Ralls

Reputation: 141

You have an array called UIDs, holding 4 unnamed objects having the attributes "ID" and "Type". To access this in javascript:

console.log("ID: " + data.UIDs[3].ID ); 

Upvotes: 1

Related Questions