2016-12-02 43 views
0

所以我们的目标是得到这样的字符串:如何使用JavaScript从多维对象数组构建URL?

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

所以我开始建立对象数组多维的事情,我不知道你会如何使用的名称来建立网址阵列组。

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。

编辑:

我想我知道什么是错的,第一你不concatinate。在JavaScript中,这是PHP和我想我必须顺序追加每个部分到一个字符串以获得URL后空格变成%20,但我仍然不知道这是否是最好的方式来做到这一点。

这是更接近,但仍然错误/详细,我得到最后的条目是有道理的,因为它们都具有相同的名称。

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

请仔细阅读本和学习对象在JavaScript中是如何工作才去任何进一步的:https://开头developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects –

+0

@ J.Titus是100%正确的,您需要停止正在做的事情并了解JS对象的工作方式。指针的提及具体告诉我你正在用一种截然不同的语言(C++,我假设),这意味着如果没有访问Mozilla开发者网络(https://开发者),你永远不会去任何地方。 Mozilla浏览器。org/en-US /)来检查他们的JS引用。 – gelliott181

+0

@ J.Titus实际上有一个由Cloudinary专门做的命令,但我需要Node.js,我的方法也很笨,因为您需要知道文件夹中的内容。但是,我仍然不知道我到底在做什么是底线。 –

回答

1

既然你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)

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

  • 基本URL应该是第一阵列
  • 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 
 
])

+0

哇,你的答案对我来说很难(不是坏的方式)一些新的东西,比如const和你写的组合函数......我已经在过去已经发现了我的问题,当我试图找到一个问题时,问。 –

+0

感谢您的回复我认为它根据我最初的要求回答了这个问题。 –

+0

另外,它结果Cloudinary输出的结果,我只是写了一些脚本来“刮”的内容和排序/缓存他们的服务器端保存在API请求。 –

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..." 
    ] 
} 

那么你就可以建立通过经过索引的阵列

喜欢这里的照片情况:

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