是否有可能建立在JavaScript / jQuery的空多维数组? [英] Is it possible to create an empty multidimensional array in javascript/jquery?

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

问题描述

我想创建一个使用Flickr的API一个非常基本的Flickr画廊。我想实现的是按标签排序我的照片。我现在用的就是jQuery.getJSON(),这样我可以解析的 flickr.photosets.getPhotos

我感兴趣的是来自Flickr获得的数据是标签和相关的每张照片的URL。这里的问题是,出了这对我来说唯一合乎逻辑的方式是创建如下格式的一个多维数组:

 数组['TAG1'] => ['URL_1','URL_2','URL_3','URL_n'];

不过,我找不到任何方法来实现这一目标。我的code是这样的:

<$p$p><$c$c>$.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',
   功能(数据){     变种imageArray = [];
     $。每个(data.photoset.photo,功能(I,项目){       imageArray [item.tags] = [item.url_sq,];     });
});

我知道,code看起来别扭,但我已经竭尽所能,而且也没有办法,我可以算出来。


解决方案

  VAR ARR = [];
改编[0] = [];
改编[0] [0] = [];
改编[0] [0] [0] =3维阵列

多二维数组有很大的差距,除非它们被正确使用。二维阵列被称为矩阵

我相信你的数据包含所谓的标签包含的标签和一个网址的空间单独字符串。

  VAR tagObject = {};
data.photoset.photo.forEach(功能(VAL){
  val.tags.split().forEach(功能(标签){
    如果(!tagObject [标签]){
      tagObject [标签] = [];
    }
    tagObject [标签] .push(val.url_sq);
  });
});
的console.log(tagObject);
/ *
  {
    海:为url1,URL2,...]
    东西:url4,...]
    ...
  }
* /

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

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.

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.

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天全站免登陆