Javascript - 从数组中提取特定对象

时间:2016-02-04 16:38:02

标签: javascript arrays node.js

新编码&学习javascript& node.js中我在这里有一个包含50个图像结果的数组:http://mistakes.io/#6204f3edd56b8891075d

我试图在该数组中提取两个特定对象:“MediaUrl”和“SourceUrl”。

例如,我可以通过body.d.results[15].MediaUrl提取单个结果,但如何提取所有50个MediaUrl或SourceUrl?我想我可以通过for for-for循环实现这个目标但不清楚如何这样做。目标是将这些对象拉出来然后能够从这些特定元素中随机挑选。我是否首先必须使用像var myArray = ["body.d.results[0].MediaUrl", body.d.results[1].MediaUrl", body.d.results[3].MediaUrl", ...]

这样的数组创建变量

非常感谢任何帮助。我一直在寻找示例和教程,但不知道如何说出我在寻找的内容。我相信这个问题与此不同:From an array of objects, extract value of a property as array因为它没有解释如何随机选择。

3 个答案:

答案 0 :(得分:0)

如果您想将它们全部存储在自己的数组中,您可以轻松地遍历数组并将结果分配给新数组:

var mediaUrlResult = [];
var sourceUrlResult = [];
for(var i = 0; i < body.d.result.length; i++){
    mediaUrlResult.push(body.d.result.MediaUrl);
    sourceUrlResult.push(body.d.result.SourceUrl);
}

这将为您提供两个包含值的数组。现在,对于随机选择,您可以执行以下操作:

Math.floor(Math.random() * Array.length)

上面将给出一个介于0和数组长度之间的整数(由于Math.floor,不包括数组长度本身(这很有用,因为实际上数组中没有项目&#39; s长度索引,否则可能会造成麻烦!))现在你可以从你的数组中提取这个随机值:

sourceUrlResult[Math.floor(Math.random() * sourceUrlResult.length)];

但比这更容易将所有结果打包到一个变量中,然后从那里开始 - 因为它已经在内存中,所以不需要复制已经存在的数据! / p>

var array = body.d.results;
var randomIndex = Math.floor(Math.random() * array.length);
console.log('src:', array[randomIndex].SourceUrl, 'media:', array[randomIndex].Media)

答案 1 :(得分:0)

要提取MediaUrl个数组,请通过map电话运行:

var mediaUrls = arr.map(function (x) {
    return x["MediaUrl"];
});

由于map&#34;创建一个新数组,其结果是在此数组中的每个元素上调用一个提供的函数&#34;,返回的是此数组:

["http://4.bp.blogspot.com/-rshrTkUD4Do/TV6mxlXoUxI/AAAAAAAAACw/6kb0msv26iA/s1600/Tina+Modotti.jpg", "http://media-cache-ec0.pinimg.com/736x/ee/cf/5f/eecf5f6d5e776104548f542bd269f631.jpg", ...]

Snippet(使用缩短版的JSON):

&#13;
&#13;
var d = [{
  "__metadata": {
    "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Photography by tina modotti'&ImageFilters='Size:Large'&$skip=0&$top=1",
    "type": "ImageResult"
  },
  "ID": "acedb185-cc7e-4da8-89be-bd107f4a6528",
  "Title": "tina modotti biography from profots com tina modotti was a remarkable ...",
  "MediaUrl": "http://4.bp.blogspot.com/-rshrTkUD4Do/TV6mxlXoUxI/AAAAAAAAACw/6kb0msv26iA/s1600/Tina+Modotti.jpg",
  "SourceUrl": "http://mastersofphotography.blogspot.com.au/2011/02/tina-modotti.html",
  "DisplayUrl": "mastersofphotography.blogspot.com.au/2011/02/tina-modotti.html",
  "Width": "530",
  "Height": "480",
  "FileSize": "87133",
  "ContentType": "image/jpeg",
  "Thumbnail": {
    "__metadata": {
      "type": "Bing.Thumbnail"
    },
    "MediaUrl": "http://ts2.mm.bing.net/th?id=OIP.M1dd1aa7a5fc5f4b73e1258d402d0f5dbH0&pid=15.1",
    "ContentType": "image/jpg",
    "Width": "480",
    "Height": "434",
    "FileSize": "27551"
  }
}, {
  "__metadata": {
    "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Photography by tina modotti'&ImageFilters='Size:Large'&$skip=1&$top=1",
    "type": "ImageResult"
  },
  "ID": "5acade4e-106e-49af-b7b4-c3e89fdd2f6d",
  "Title": "Tina Modotti | Indigenous | Pinterest",
  "MediaUrl": "http://media-cache-ec0.pinimg.com/736x/ee/cf/5f/eecf5f6d5e776104548f542bd269f631.jpg",
  "SourceUrl": "http://pinterest.com/pin/393994667370497691/",
  "DisplayUrl": "pinterest.com/pin/393994667370497691",
  "Width": "654",
  "Height": "720",
  "FileSize": "168465",
  "ContentType": "image/jpeg",
  "Thumbnail": {
    "__metadata": {
      "type": "Bing.Thumbnail"
    },
    "MediaUrl": "http://ts4.mm.bing.net/th?id=OIP.M94668199c6351c86fc3d8a8267c19602o2&pid=15.1",
    "ContentType": "image/jpg",
    "Width": "272",
    "Height": "300",
    "FileSize": "11661"
  }
}, {
  "__metadata": {
    "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Photography by tina modotti'&ImageFilters='Size:Large'&$skip=2&$top=1",
    "type": "ImageResult"
  },
  "ID": "aa661abe-dedc-47c4-a524-fa83e1efbdb2",
  "Title": "Tina Modotti",
  "MediaUrl": "http://www.masters-of-photography.com/images/full/modotti/modotti_roses.jpg",
  "SourceUrl": "http://www.masters-of-photography.com/M/modotti/modotti_roses_full.html",
  "DisplayUrl": "www.masters-of-photography.com/M/modotti/modotti_roses_full.html",
  "Width": "610",
  "Height": "480",
  "FileSize": "78365",
  "ContentType": "image/jpeg",
  "Thumbnail": {
    "__metadata": {
      "type": "Bing.Thumbnail"
    },
    "MediaUrl": "http://ts1.mm.bing.net/th?id=OIP.Maeca6f4759d245091ae2276829dca4efo0&pid=15.1",
    "ContentType": "image/jpg",
    "Width": "300",
    "Height": "236",
    "FileSize": "8950"
  }
}];

console.log(d.map(function(x) {
  return x["MediaUrl"];
}));
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这就是我这样做的方式。

var newArray = body.d.results.map(function(obj){
   return {MediaUrl: obj['MediaUrl'], SourceUrl: obj['SourceUrl']}
});
function getFromRandomArray(array) {
   return array[Math.floor(Math.random() * Array.length)]
}

并且只要您想要随机值getFromArray(newArray)