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

后端 未结 8 2224
粉色の甜心
粉色の甜心 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:38

    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.

提交回复
热议问题