admin管理员组文章数量:1178539
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('/?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.
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.
Share Improve this question edited Jul 17, 2014 at 9:18 Artjom B. 61.9k25 gold badges134 silver badges229 bronze badges asked Sep 22, 2011 at 21:35 finferflufinferflu 1,3782 gold badges11 silver badges28 bronze badges 2- Can you show us the response and what it looks like? That'll help significantly. – g.d.d.c Commented Sep 22, 2011 at 21:37
- That's the response: pastebin.com/S5g2zwwD – finferflu Commented Sep 22, 2011 at 21:40
8 Answers
Reset to default 15var 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.
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
...
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.
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/", ...],
...
//}
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] = [];
}
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)
Even though the definition of an array in JavaScript is foggy at best.
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.
Yes, it's possible to have multidimensional arrays in javascript.
Here are one-liners for 5x5 matrix:
Array(5).fill(0).map(() => new Array(5).fill(0))
Or
[...Array(5)].map(x=>Array(5).fill(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);
});
You can simply try this one for 2d js array:
let myArray = [[]];
本文标签: Is it possible to create an empty multidimensional array in javascriptjqueryStack Overflow
版权声明:本文标题:Is it possible to create an empty multidimensional array in javascriptjquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738047508a2054876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论