是否可以在 javascript/jquery 中创建一个空的多维数组? [英] Is it possible to create an empty multidimensional array in javascript/jquery?

查看:29
本文介绍了是否可以在 javascript/jquery 中创建一个空的多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Flickr API 创建一个非常基本的 Flickr 画廊.我想要实现的是按标签对我的图片进行排序.我使用的是 jQuery.getJSON() 以便我可以解析 flickr.photosets.getPhotos.

I am trying to create a very basic Flickr gallery using the Flickr API. What I want to achieve is sorting my pictures by tag. What I am using is jQuery.getJSON() so that I can parse the API response of flickr.photosets.getPhotos.

我有兴趣从 Flickr 获取的数据是与每张照片相关联的标签和 URL.问题在于,对我来说,唯一合乎逻辑的方法是创建以下格式的多维数组:

The data I am interested in getting from Flickr is the tag and the URL associated to each photo. The problem with this is that the only logical way out of this for me is creating a multidimensional array of the following format:

Array['tag1'] => ['URL_1', 'URL_2', 'URL_3', 'URL_n'];

但是,我找不到任何方法来实现这一目标.我的代码如下所示:

However, I cannot find any way to achieve this. My code looks like this:

$.getJSON('http://api.flickr.com/services/rest/?api_key=xxx&method=flickr.photosets.getPhotos&user_id=xxx&format=json&extras=tags%2C+url_l%2C+url_sq&nojsoncallback=1&photoset_id=xxx', 
   function(data) {

     var imageArray = [];   
     $.each(data.photoset.photo, function(i, item) {

       imageArray[item.tags] = [item.url_sq,];

     });
});

我知道代码可能看起来很笨拙,但我已经尝试了所有方法,但无法弄清楚这一点.

I am aware that the code might look awkward, but I've tried everything and there's no way I can figure this out.

推荐答案

var arr = [];
arr[0] = [];
arr[0][0] = [];
arr[0][0][0] = "3 dimentional array"

除非使用得当,否则多维数组会有很多空白.二维数组称为矩阵.

Multi dimentional arrays have a lot of gaps unless they are used properly. A two dimensional array is called a matrix.

我相信您的数据包含一个名为tags"的空格分隔字符串,其中包含标签和一个网址.

I believe your data contains a space seperate string called "tags" containing the tags and a single url.

var tagObject = {};
data.photoset.photo.forEach(function(val) {
  val.tags.split(" ").forEach(function(tag) {
    if (!tagObject[tag]) {
      tagObject[tag] = [];
    }
    tagObject[tag].push(val.url_sq);
  });
});
console.log(tagObject); 
/*
  {
    "sea": ["url1", "url2", ...],
    "things": ["url4", ...],
    ...
  }
*/

我不知道它是如何返回多个标签的.

I don't know how it returns multiple tags.

这篇关于是否可以在 javascript/jquery 中创建一个空的多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆