如何在jQuery中通过索引访问键的值?

时间:2022-11-12 14:54:00

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?

以下面的例子为例,它是json响应的一部分。我想访问第四个“ID”的“TestUser4”值。我怎样才能用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 );
    }
});

2 个解决方案

#1


1  

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

你有一个名为UID的数组,它包含4个具有属性“ID”和“Type”的未命名对象。要在javascript中访问它:

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

#2


1  

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

你非常接近..你想要做的是在UIDs数组的第三个索引处访问ID,如下所示:

data.UIDs[3].ID

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..

希望这可以帮助..

#1


1  

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

你有一个名为UID的数组,它包含4个具有属性“ID”和“Type”的未命名对象。要在javascript中访问它:

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

#2


1  

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

你非常接近..你想要做的是在UIDs数组的第三个索引处访问ID,如下所示:

data.UIDs[3].ID

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..

希望这可以帮助..