如何在ajax中获取我的数组的值

时间:2015-08-18 02:58:33

标签: javascript jquery arrays ajax

这是我获取jsonp请求的代码。

  $.ajax({
        type: "GET",
        url: pbxApi+"/confbridge_participants/conference_participants.json?cid="+circle,
        dataType: "jsonp",
        jsonpCallback: 'callback',
        contentType: "application/javascript",
        success: function(data) {

         console.log(data);

        }
        });

这是请求

enter image description here

它输出数组中的所有数据。

我想仅输出特定值

喜欢uid的价值。

我希望为返回的每个对象输出uid。我该怎么办?

3 个答案:

答案 0 :(得分:1)

如果你要做的就是打印每个对象的uid,你可以迭代对象数组并单独访问每个uid

$.ajax({
    type: "GET",
    url: pbxApi+"/confbridge_participants/conference_participants.json?cid="+circle,
    dataType: "jsonp",
    jsonpCallback: 'callback',
    contentType: "application/javascript",
    success: function(data) {
        for( var obj in data ) {
            // Accessing object property using dot notation
            console.log(obj.uid);
            // Or square bracket notation
            console.log(obj['uid']);
        }
    }
    });

答案 1 :(得分:1)

尝试使用.each(),因为此函数将迭代data变量:

 $.each(data, function(i,e){
   // do something here 
   console.log(e.id);
 });

答案 2 :(得分:1)

您可以使用map()函数返回数组的新修改版本;

console.log(data.map(function(o) { return o.id; }));