Jquery - 对象中的$ .each链接

时间:2011-09-26 19:05:52

标签: jquery ajax json api

我有以下API示例:

"urls": [
{
"value": "http://twitter.com"
},
{
"value": "http://gplus.to"
},
{
"value": "http://plus.ly"
},
{
"value": "http://glpl.us"
},
{
"value": "http://microsoft.ms/+"
},
{
"value": "https://plus.google.com",
"type": "profile"
},
{
"value": "https://www.googleapis.com/plus/v1/people",
"type": "json"
}
]

如何使用.each()显示对象中链接值的每个href?

另外,如何使用type = profiletype = json排除值?

我试过了:

  var yourLinks = data.urls;

  $.each(yourLinks, function(key, value) { 
    alert(key + ': ' + value); 
  });

但警报只包含object : object

2 个答案:

答案 0 :(得分:3)

alert(value.value); //<<<try that

或者更详细:

  var yourLinks = data.urls;

  $.each(yourLinks, function(index, vals) { 
    alert(index + ":" + vals.value); 
  });

答案 1 :(得分:-1)

正在发生的事情是data.urls的每个元素都是一个对象,所以你基本上需要运行一个double-each来获取每个元素的键/值对:

$.each(data.urls, function(index, obj) {
  $.each(obj, function(key, value) {
    alert(key + ': ' + value);
  });
});