For loop in javascript with empty body - 有更好的方法吗?
For loop in javascript with empty body - is there a better way to do it?
我有这样一个代码:
var ccode = [
["de", "de", ".de.example.com"],
["uk", "uk", ".uk.example.com"],
["uk", "nl", ".nl.example.com"],
["pl", "pl", ".pl.example.com"]
];
var lng;
var gamepage;
var reg;
var gamepages = {}
{
var i;
for (i = 0; i < ccode.length && !(document.location.href.search(ccode[i][1] + ".example.com") != -1); i++) {}
lng = ccode[i][0];
gamepage = ccode[i][2];
reg = new RegExp("http://s(\d+)\." + ccode[i][1] + "\.example\.com/(.*?)\.php(.*)", "i");
gamepages[ccode[i][1]] = "http://www" + ccode[i][2];
}
它有效但是..它看起来很糟糕,有更好的方法吗?如果您需要背景,它会在页面的 URL 地址中查找 "de" 或 "en" 并设置变量以更改语言。
感谢您的帮助。 ;)
您想对 ccode
上的每个项目做点什么,对吗? for
意味着很多样板...试试 [].forEach()
!
var ccode = [
["de", "de", ".de.example.com"],
["uk", "uk", ".uk.example.com"],
["uk", "nl", ".nl.example.com"],
["pl", "pl", ".pl.example.com"]
];
ccode.forEach(function(c) {
// c will be ["de", "de", ".de.example.com"] at first, and so on...
if ((document.location.href.search(c + ".example.com") != -1)) return;
console.log(c)
})
ccode.forEach(function(code){
if(document.location.href.search(code[1] + "example.com"){
lng = code[0];
}
});
我就是这样解决的。避免for循环,意图明确
您可以使用 for...in
循环。在数组上使用 for...in
循环只会迭代数组的可枚举属性。
然后我会使用 match
来测试 url,因为您可以使用 !!
.
可靠地从结果中强制转换布尔值
我添加了默认数组选项,否则它会使用迭代的最后一个项目,即使没有匹配的项目。这样您就可以明确地告诉脚本在没有匹配项时停止(或以其他方式处理条件)。
此外,一些 RegEx 简化。
var lng, gamepage, reg, gamepages = {}
var ccode = [
["de", "de", ".de.example.com"],
["uk", "uk", ".uk.example.com"],
["uk", "nl", ".nl.example.com"],
["pl", "pl", ".pl.example.com"],
["", "", ""] // default to signal if nothing found.
];
// Loop the array Test the locatio, if it matches break the loop
for(var i in ccode) !!location.href.match(ccode[i][1]+'.example.com') && break;
if(!ccode[i][0]) return; // stop now if there was nothing found.
lng = ccode[i][0];
gamepage = ccode[i][2];
reg = new RegExp("http://s(\d+)."+ccode[i][1]+".example.com/([^.]+).php(.*)", "i");
gamepages[ccode[i][1]] = "http://www" + ccode[i][2];
我有这样一个代码:
var ccode = [
["de", "de", ".de.example.com"],
["uk", "uk", ".uk.example.com"],
["uk", "nl", ".nl.example.com"],
["pl", "pl", ".pl.example.com"]
];
var lng;
var gamepage;
var reg;
var gamepages = {}
{
var i;
for (i = 0; i < ccode.length && !(document.location.href.search(ccode[i][1] + ".example.com") != -1); i++) {}
lng = ccode[i][0];
gamepage = ccode[i][2];
reg = new RegExp("http://s(\d+)\." + ccode[i][1] + "\.example\.com/(.*?)\.php(.*)", "i");
gamepages[ccode[i][1]] = "http://www" + ccode[i][2];
}
它有效但是..它看起来很糟糕,有更好的方法吗?如果您需要背景,它会在页面的 URL 地址中查找 "de" 或 "en" 并设置变量以更改语言。
感谢您的帮助。 ;)
您想对 ccode
上的每个项目做点什么,对吗? for
意味着很多样板...试试 [].forEach()
!
var ccode = [
["de", "de", ".de.example.com"],
["uk", "uk", ".uk.example.com"],
["uk", "nl", ".nl.example.com"],
["pl", "pl", ".pl.example.com"]
];
ccode.forEach(function(c) {
// c will be ["de", "de", ".de.example.com"] at first, and so on...
if ((document.location.href.search(c + ".example.com") != -1)) return;
console.log(c)
})
ccode.forEach(function(code){
if(document.location.href.search(code[1] + "example.com"){
lng = code[0];
}
});
我就是这样解决的。避免for循环,意图明确
您可以使用 for...in
循环。在数组上使用 for...in
循环只会迭代数组的可枚举属性。
然后我会使用 match
来测试 url,因为您可以使用 !!
.
我添加了默认数组选项,否则它会使用迭代的最后一个项目,即使没有匹配的项目。这样您就可以明确地告诉脚本在没有匹配项时停止(或以其他方式处理条件)。
此外,一些 RegEx 简化。
var lng, gamepage, reg, gamepages = {}
var ccode = [
["de", "de", ".de.example.com"],
["uk", "uk", ".uk.example.com"],
["uk", "nl", ".nl.example.com"],
["pl", "pl", ".pl.example.com"],
["", "", ""] // default to signal if nothing found.
];
// Loop the array Test the locatio, if it matches break the loop
for(var i in ccode) !!location.href.match(ccode[i][1]+'.example.com') && break;
if(!ccode[i][0]) return; // stop now if there was nothing found.
lng = ccode[i][0];
gamepage = ccode[i][2];
reg = new RegExp("http://s(\d+)."+ccode[i][1]+".example.com/([^.]+).php(.*)", "i");
gamepages[ccode[i][1]] = "http://www" + ccode[i][2];