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

后端 未结 8 2198
粉色の甜心
粉色の甜心 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.

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

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