是否可以在javascript/jquery中创建一个空的多维数组?

时间:2021-02-16 21:28:40

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 API创建一个非常基本的Flickr图库。我想要实现的是通过标签对图片进行分类。我使用的是jQuery.getJSON(),这样我就可以解析flickr.photosets.getPhotos的API响应。

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:

我想从Flickr获取的数据是标签和每个照片的URL。这样做的问题是,对我来说,唯一合乎逻辑的方法是创建一个多维数组,格式如下:

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.

我知道这段代码可能看起来很笨拙,但是我已经尝试了所有的东西,我不可能弄清楚。

6 个解决方案

#1


14  

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.

我相信您的数据包含一个名为“tags”的分隔字符串,其中包含标签和一个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.

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

#2


1  

I think the syntax you are attempting to achieve is something like the following:

我认为您试图实现的语法如下:

var item = {"tags":"blah","url_sq":"example.com"}; // for sake of example.
var imageArray = [];
$.each(data.photoset.photo, function(i, item) {
   imageArray.push({"tags":item.tags,"url":item.url_sq});
});

and then reference it like this:

然后这样引用它:

imageArray[0].tags
imageArray[0].url
imageArray[1].tags
imageArray[1].url
...

#3


1  

JavaScript doesn't have true multidimensional arrays (heck, it doesn't even have true regular arrays...), but, like most languages, it instead uses arrays of arrays. However, to me it looks like you need an object (kinda similar to PHP's arrays) containing arrays.

JavaScript没有真正的多维数组(见鬼,它甚至没有真正的常规数组…),但是,像大多数语言一样,它使用数组。但是,在我看来,您需要一个包含数组的对象(有点类似于PHP的数组)。

var data = {
    tag1: ['URL_1', 'URL_2', 'URL_3', 'URL_n']
};
// Then accessed like:
data.tag1; // ['URL_1', ...]
data.tag1[0]; // 'URL_1'
data.tag1[1]; // 'URL_2'
// etc.

So, you're problem would look something like this:

所以,你的问题是这样的:

var tags = {};
$.each(data.photoset.photo, function (i, item) {
    $.each(item.tags.split(" "), function (i, tag) {
        if (!tags[tag])
            tags[tag] = [];
        tags[tag].push(item.url_sq);
    });
});
// tags is now something like:
// {
    "foo": ["/url.html", "/other-url/", ...],
    "bar": ["/yes-im-a-lisp-programmer-and-thats-why-i-hypenate-things-that-others-might-underscore-or-camelcase/", ...],
    ...
//}

#4


1  

Maybe something like that in your each:

也许你们每个人身上都有这样的东西:

if ( imageArray[item.tags] != null ){
   imageArray[item.tags][imageArray[item.tags].length] = item.url_sq;
}else{
   imageArray[item.tags] = [];
}

#5


1  

Yes it is! I found this to be quite fast. I might stretch to say that it could be the fastest way possible to generate a N Dimensional array of Ns lengths resulting in empty arrays using JavaScript. (i.e. an arbitrary number of dimensions each with an arbitrary length)

没错,是很好玩!我发现这个速度很快。我想说的是,这可能是生成N维N长度数组的最快方法,使用JavaScript生成空数组。(即任意长度的任意维数)

Even though the definition of an array in JavaScript is foggy at best.

即使在JavaScript中数组的定义是模糊的。

function createNDimArray(dimensions) {
    var t, i = 0, s = dimensions[0], arr = new Array(s);
    if ( dimensions.length < 3 ) for ( t = dimensions[1] ; i < s ; ) arr[i++] = new Array(t);
    else for ( t = dimensions.slice(1) ; i < s ; ) arr[i++] = createNDimArray(t);
    return arr;
}

Usages:

用法:

var arr = createNDimArray([3, 2, 3]); 
//  arr = [[[,,],[,,]],[[,,],[,,]],[[,,],[,,]]]
console.log(arr[2][1]); // in FF: Array [ <3 empty slots> ]
console.log("Falsy = " + (arr[2][1][0]?true:false) ); // Falsy = false

If you care to read more; check my answer to this question.

如果你想多读一些;检查我对这个问题的回答。

#6


0  

I think something like this should do what you want

我觉得像这样的东西应该做你想做的

 var imageArray = [];   

 $.each(data.photoset.photo, function(i, item) {

   // if the tag is new, then create an array
   if(!imageArray[item.tags])
     imageArray[item.tags] = [];

   // push the item url onto the tag
   imageArray[item.tags].push(item.url_sq);

 });

#1


14  

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.

我相信您的数据包含一个名为“tags”的分隔字符串,其中包含标签和一个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.

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

#2


1  

I think the syntax you are attempting to achieve is something like the following:

我认为您试图实现的语法如下:

var item = {"tags":"blah","url_sq":"example.com"}; // for sake of example.
var imageArray = [];
$.each(data.photoset.photo, function(i, item) {
   imageArray.push({"tags":item.tags,"url":item.url_sq});
});

and then reference it like this:

然后这样引用它:

imageArray[0].tags
imageArray[0].url
imageArray[1].tags
imageArray[1].url
...

#3


1  

JavaScript doesn't have true multidimensional arrays (heck, it doesn't even have true regular arrays...), but, like most languages, it instead uses arrays of arrays. However, to me it looks like you need an object (kinda similar to PHP's arrays) containing arrays.

JavaScript没有真正的多维数组(见鬼,它甚至没有真正的常规数组…),但是,像大多数语言一样,它使用数组。但是,在我看来,您需要一个包含数组的对象(有点类似于PHP的数组)。

var data = {
    tag1: ['URL_1', 'URL_2', 'URL_3', 'URL_n']
};
// Then accessed like:
data.tag1; // ['URL_1', ...]
data.tag1[0]; // 'URL_1'
data.tag1[1]; // 'URL_2'
// etc.

So, you're problem would look something like this:

所以,你的问题是这样的:

var tags = {};
$.each(data.photoset.photo, function (i, item) {
    $.each(item.tags.split(" "), function (i, tag) {
        if (!tags[tag])
            tags[tag] = [];
        tags[tag].push(item.url_sq);
    });
});
// tags is now something like:
// {
    "foo": ["/url.html", "/other-url/", ...],
    "bar": ["/yes-im-a-lisp-programmer-and-thats-why-i-hypenate-things-that-others-might-underscore-or-camelcase/", ...],
    ...
//}

#4


1  

Maybe something like that in your each:

也许你们每个人身上都有这样的东西:

if ( imageArray[item.tags] != null ){
   imageArray[item.tags][imageArray[item.tags].length] = item.url_sq;
}else{
   imageArray[item.tags] = [];
}

#5


1  

Yes it is! I found this to be quite fast. I might stretch to say that it could be the fastest way possible to generate a N Dimensional array of Ns lengths resulting in empty arrays using JavaScript. (i.e. an arbitrary number of dimensions each with an arbitrary length)

没错,是很好玩!我发现这个速度很快。我想说的是,这可能是生成N维N长度数组的最快方法,使用JavaScript生成空数组。(即任意长度的任意维数)

Even though the definition of an array in JavaScript is foggy at best.

即使在JavaScript中数组的定义是模糊的。

function createNDimArray(dimensions) {
    var t, i = 0, s = dimensions[0], arr = new Array(s);
    if ( dimensions.length < 3 ) for ( t = dimensions[1] ; i < s ; ) arr[i++] = new Array(t);
    else for ( t = dimensions.slice(1) ; i < s ; ) arr[i++] = createNDimArray(t);
    return arr;
}

Usages:

用法:

var arr = createNDimArray([3, 2, 3]); 
//  arr = [[[,,],[,,]],[[,,],[,,]],[[,,],[,,]]]
console.log(arr[2][1]); // in FF: Array [ <3 empty slots> ]
console.log("Falsy = " + (arr[2][1][0]?true:false) ); // Falsy = false

If you care to read more; check my answer to this question.

如果你想多读一些;检查我对这个问题的回答。

#6


0  

I think something like this should do what you want

我觉得像这样的东西应该做你想做的

 var imageArray = [];   

 $.each(data.photoset.photo, function(i, item) {

   // if the tag is new, then create an array
   if(!imageArray[item.tags])
     imageArray[item.tags] = [];

   // push the item url onto the tag
   imageArray[item.tags].push(item.url_sq);

 });