Is it possible to create an empty multidimensional array in javascript/jquery?

后端 未结 8 2197
粉色の甜心
粉色の甜心 2020-12-06 10:36

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 c

相关标签:
8条回答
  • 2020-12-06 11:19

    You can simply try this one for 2d js array:

    let myArray = [[]];
    
    0 讨论(0)
  • 2020-12-06 11:20

    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
    ...
    
    0 讨论(0)
  • 2020-12-06 11:26

    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/", ...],
        ...
    //}
    
    0 讨论(0)
  • 2020-12-06 11:30

    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.

    0 讨论(0)
  • 2020-12-06 11:32

    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))

    0 讨论(0)
  • 2020-12-06 11:34

    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] = [];
    }
    
    0 讨论(0)
提交回复
热议问题