繁体   English   中英

如何使用JavaScript从多维对象数组构建URL?

[英]How to build URL from multi-dimensional object array with JavaScript?

因此,目标是获得这样的字符串:

somesite.com/-%20Luzon%20-/Bicol%20Region/Albay/Busay%20Falls/Busay_falls_10.jpg

因此,我开始构建对象数组多维对象,但不确定如何使用数组组的名称来构建url。

    var photoArray   = {},
        islandGroups = ["- Luzon -", " - Visayas -", "-Mindanao-"],
        luzonRegions = ["Bicol Region", "Cagayan Valley", "Calabarzon", "CAR", "Central Luzon", "Ilocos Region", "Mimaropa"],
        bicolProvinces = ["Albay", "Camarines Norte", "Camarines Sur", "Catanduanes", "Masbate", "Sorsogon"],
        albayProvinceTravelDestinations = ["Busay Falls", "Hoyop-Hoyopan Cave", "Lignon Hill", "Malabsay Falls", "Mt Mayon", "Panicuason Hot Spring Resort", "Vera Falls"];
        busayFallsPhotoPattern = ["Cover, Busay_falls_"];
        busayFallsPhotoPatternID1 = [""];
        busayFallsPhotoPatternID2 = ["1", "1_2", "10", "2", "2_2", "3", "3_2", "4", "4_2", "6", "7", "8", "9"];
        baseURL = "somesite.com";
    /* build sub-array */
    var islandGroupsArrayLength = islandGroups.length;
    for (var i = 0; i < islandGroupsArrayLength; i++) {
      photoArray[islandGroups[i]] = {};
    }
    var luzonRegionsLength = luzonRegions.length;
    for (var i = 0; i < luzonRegionsLength; i++) {
      photoArray["- Luzon -"][luzonRegions[i]] = {};
    }
    var bicolProvincesLength = bicolProvinces.length;
    for (var i = 0; i < bicolProvincesLength; i++) {
      photoArray["- Luzon -"]["Bicol Region"][bicolProvinces[i]] = {};
    }
    var albayProvinceTravelDestinationsLength = albayProvinceTravelDestinations.length;
    for (var i = 0; i < albayProvinceTravelDestinations; i++) {
      photoArray["- Luzon -"]["Bicol Region"]["Albay"][i] = {};
    }
    /* build string before converting space to %20% */
    /*
    busayFallsPhotoPattern = ["Cover, Busay_falls_"];
        busayFallsPhotoPatternID1 = [""];
        busayFallsPhotoPatternID2 = ["1", "1_2", "10", "2", "2_2", "3", "3_2", "4", "4_2", "6", "7", "8", "9"];
    */
    var busayFallsPhotoPatternLength = busayFallsPhotoPattern.length,
        busayFallsPhotoPatternID2Length = busayFallsPhotoPatternID2.length;
    /* setup photoURLs array */
    photoArray["- Luzon -"]["Bicol Region"]["Albay"]["Busay Falls"] = {};
    photoArray["- Luzon -"]["Bicol Region"]["Albay"]["Busay Falls"]["busayFallsPhotoURLs"] = {};

    photoArray["- Luzon -"]["Bicol Region"]["Albay"]["Busay Falls"]["busayFallsPhotoURLs"][0] = "Cover" + ".jpg";

    console.log(photoArray.[0].[0].[0].[0].[0].[0]); // this doesn't work

可能显而易见,此刻我的想法有点不知所措

我也想知道如何更有效地构建它/使用指针,以便例如没有superLongNameLength。

编辑:

我想我知道这是怎么回事,首先您不要夸大其词。 在PHP的JavaScript中,我还想将空格转换成%20后必须将每个片段顺序附加到字符串上,以获取url,但是我仍然不确定这是否是最佳方法。

这更接近,但仍然是错误/冗长,我得到的最后一个条目是有意义的,因为它们都具有相同的名称。

var islandGroups = ["- Luzon -", "- Visayas -", "-Mindanao"],
    regions     = {
      "- Luzon -" : "Bicol Region", 
      "- Luzon -" : "Cagayan Valley",
      "- Luzon -" : "Calabarzon",
      "- Luzon -" : "CAR",
      "- Luzon -" : "Central Luzon",
      "- Luzon -" : "Ilocos Region",
      "- Luzon -" : "Mimaropa"
    },
    provinces = {
      "Bicol Region" : "Albay",
      "Bicol Region" : "Camarines Norte",
      "Bicol Region" : "Camarines Sur",
      "Bicol Region" : "Catanduanes",
      "Bicol Region" : "Masbate",
      "Bicol Region" : "Sorsogon"
    },
    travelDestinations = {
      "Albay" : "Busay Falls",
      "Albay" : "Hoyop-Hoyopan Cave",
      "Albay" : "Lignon Hill",
      "Albay" : "Malabsay Falls",
      "Albay" : "Mt Mayon",
      "Albay" : "Panicuason Hot Spring Resort",
      "Albay" : "Vera Falls"
    },
    photos = {
      "Busay Falls" : "Cover.jpg",
      "Busay Falls" : "Busay_falls_10.jpg",
      "Busay Falls" : "Busay_falls_2.jpg",
      "Busay Falls" : "Busay_falls_3.jpg",
      "Busay Falls" : "Busay_falls_4.jpg",
      "Busay Falls" : "Busay_falls_5.jpg",
      "Busay Falls" : "Busay_falls_6.jpg",
      "Busay Falls" : "Busay_falls_7.jpg",
      "Busay Falls" : "Busay_falls_8.jpg",
      "Busay Falls" : "Busay_falls_9.jpg"
    };

    console.log(islandGroups[0]+regions["- Luzon -"]+provinces["Bicol Region"]+travelDestinations["Albay"]+photos["Busay Falls"]);

是的,那里绝对是完全错误的,键是相同的...因此,而不是例如:

photos = {
  "Busay Falls" : "Cover.jpg",
  "Busay Falls" : "Busay_falls_10.jpg",
  "Busay Falls" : "etc..."
}

它应该是

photos = {
  "busay_falls":[
    "Cover.jpg",
    "Busay_falls_10.jpg",
    "etc..."
  ]
}

那么您可以通过遍历数组的索引来构建url

喜欢这里的照片盒:

photos.busay_falls[0] // becomes Cover.jpg

由于您有n数组,每个数组具有可变数量的项,因此,您最好的办法是递归地从每个数组中选择一个元素,如下所示:

 const lower = ['a', 'b', 'c'] const upper = ['X', 'Y', 'Z'] const numbers = [1, 2, 3] const all = [lower, upper, numbers] // all = a 2d array // current = the i-th array of `all` we're currently operating on // result = the concatenated string function combination(all, current = 0, result = '') { if (current == all.length) { console.log(result) return } for (let i = 0; i < all[current].length; i += 1) { // concatenate a new string from the i-th array combination(all, current + 1, result + all[current][i] + '/') } } combination(all) 

将上述算法应用于字符串需要进行一些修改:

  • 基本网址应该是第一个数组
  • result应该被编码

 const baseURL = ["somesite.com"]; const islandGroups = ["- Luzon -", " - Visayas -", "-Mindanao-"]; const luzonRegions = ["Bicol Region", "Cagayan Valley", "Calabarzon", "CAR", "Central Luzon", "Ilocos Region", "Mimaropa"]; const bicolProvinces = ["Albay", "Camarines Norte", "Camarines Sur", "Catanduanes", "Masbate", "Sorsogon"]; const albayProvinceTravelDestinations = ["Busay Falls", "Hoyop-Hoyopan Cave", "Lignon Hill", "Malabsay Falls", "Mt Mayon", "Panicuason Hot Spring Resort", "Vera Falls"]; const busayFallsPhotoPattern = ["Cover", "Busay_falls_"]; const busayFallsPhotoPatternID2 = ["1", "1_2", "10", "2", "2_2", "3", "3_2", "4", "4_2", "6", "7", "8", "9"]; // all = a 2d array // current = the i-th array of `all` we're currently operating on // result = the concatenated string function combination(all, current = 0, result = '') { if (current == all.length) { // add extension console.log(result + '.jpg') return } for (let i = 0; i < all[current].length; i += 1) { // concatenate a new string from the i-th array let newString = encodeURI(result + all[current][i]) // empty string shouldn't have / // when current >= all.length - 2 we shouldn't add / if (all[current][i] && current < all.length - 2) { newString += '/' } combination(all, current + 1, newString) } } combination([ baseURL, islandGroups, luzonRegions, bicolProvinces, albayProvinceTravelDestinations, busayFallsPhotoPattern, busayFallsPhotoPatternID2 ]) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM