如何从数组中的对象访问该值?

时间:2022-05-16 08:57:13

When i loop from a rss data I want to extract the url value from an object.

当我从rss数据中循环时,我想从一个对象中提取url值。

$.ajax({
 url      : 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=30&callback=?&q=' + encodeURIComponent('http://www.dnoticias.pt/rss/actualidade/geral'),
 dataType : 'json',
 success  : function (data) {
   if (data.responseData.feed && data.responseData.feed.entries) {
   $.each(data.responseData.feed.entries, function (i, e) {

     title = e.title;
     description = e.description;
     category = e.categories[0];
     media = e.mediaGroups;
     console.log(media);
   }
  }
});

Here is the output from the console log:

以下是控制台日志的输出:

mediaGroups: Array[1]
 0: Object
  contents: Array[1]
   0: Object
    type: "image/jpeg"
    url: "http://...."

Thanks

谢谢

3 个解决方案

#1


1  

From the output you've shown mediaGroups is an array of objects, which themselves contain a contents property which is another array of objects, so you need to use nested loops to pull out each url property from those objects.

从您已经显示的输出中,mediaGroups是一个对象数组,它本身包含一个contents属性,这是另一个对象数组,因此需要使用嵌套循环从这些对象中提取每个url属性。

$.each(data.responseData.feed.entries, function(_, entry) {
    $.each(entry.mediaGroups, function(_, mediaGroup) {
        $.each(mediaGroup.contents, function(_, content) {
            var url = content.url;
            // use url as required here...
        });
    });
});

If you always only ever want to retrieve the first item from the array, and can guarantee that each child array will contain at least one item, then you can simply access them by index like this:

如果您总是只想从数组中检索第一个项,并且可以保证每个子数组至少包含一个项,那么您可以通过这样的索引来访问它们:

$.each(data.responseData.feed.entries, function (i, entry) {
    var url = entry.mediaGroups[0].contents[0].url;
    // use url as required here...
});

Working example

工作示例

#2


0  

You just access array elements by indexing, and property by property names:

您只需通过索引访问数组元素,并通过属性名访问属性:

 e.mediaGroups[0].contents[0].type

#3


0  

if the arrays have only one element, then it's as simple as this:

如果数组只有一个元素,那么简单如下:

media = e.mediaGroups[0];
contents = media.contents[0];

then you can do somehting like this:

然后你可以这样做:

someVar1 = contents.type;
someVar2 = contents.url;

otherwise you will have to iterate through mediaGroups and through contents as well

否则,您将不得不遍历mediaGroups和内容。

#1


1  

From the output you've shown mediaGroups is an array of objects, which themselves contain a contents property which is another array of objects, so you need to use nested loops to pull out each url property from those objects.

从您已经显示的输出中,mediaGroups是一个对象数组,它本身包含一个contents属性,这是另一个对象数组,因此需要使用嵌套循环从这些对象中提取每个url属性。

$.each(data.responseData.feed.entries, function(_, entry) {
    $.each(entry.mediaGroups, function(_, mediaGroup) {
        $.each(mediaGroup.contents, function(_, content) {
            var url = content.url;
            // use url as required here...
        });
    });
});

If you always only ever want to retrieve the first item from the array, and can guarantee that each child array will contain at least one item, then you can simply access them by index like this:

如果您总是只想从数组中检索第一个项,并且可以保证每个子数组至少包含一个项,那么您可以通过这样的索引来访问它们:

$.each(data.responseData.feed.entries, function (i, entry) {
    var url = entry.mediaGroups[0].contents[0].url;
    // use url as required here...
});

Working example

工作示例

#2


0  

You just access array elements by indexing, and property by property names:

您只需通过索引访问数组元素,并通过属性名访问属性:

 e.mediaGroups[0].contents[0].type

#3


0  

if the arrays have only one element, then it's as simple as this:

如果数组只有一个元素,那么简单如下:

media = e.mediaGroups[0];
contents = media.contents[0];

then you can do somehting like this:

然后你可以这样做:

someVar1 = contents.type;
someVar2 = contents.url;

otherwise you will have to iterate through mediaGroups and through contents as well

否则,您将不得不遍历mediaGroups和内容。