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

时间:2016-12-02 18:36:15

标签: javascript arrays

所以我们的目标是得到一个像这样的字符串:

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的PHP中,我认为我必须在将空格转换为%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"]);

2 个答案:

答案 0 :(得分:1)

是的,那里肯定是完全错误的,键是相同的...所以不是例如:

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

答案 1 :(得分:1)

由于你有n个数组,每个数组都有可变数量的项目,你可以做的最好的事情是递归地选择每个数组的一个元素,如下所示:

&#13;
&#13;
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)
&#13;
&#13;
&#13;

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

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

&#13;
&#13;
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
])
&#13;
&#13;
&#13;