简体   繁体   English

用空的主体在javascript中循环-有更好的方法吗?

[英]For loop in javascript with empty body - is there a better way to do it?

I've got a code like this one: 我有这样的代码:

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];
}

It works but.. It look like crap, is there a better way to do it? 它可以工作,但是..看起来像胡扯,有更好的方法吗? If you need a background it looks for "de" or "en" in the page's URL adress and sets the variables to change the language. 如果需要背景,它将在页面的URL地址中查找“ de”或“ en”,并设置变量以更改语言。

Thank for your help. 谢谢您帮忙。 ;) ;)

You want to do something with each of the items on ccode , right? 您想对ccode上的每个项目执行ccode ,对不对? for means a lot of boilerplate just for that... try [].forEach() ! 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];
    }
});

This is how I would solve it. 这就是我要解决的方法。 For loop is avoided, and intention is clear. 避免了for循环,意图很明确。

You can use a for...in loop. 您可以使用for...in循环。 Using a for...in loop on an array only iterates the enumerable properties of the array. for...in数组上使用for...in循环只会迭代数组的可枚举属性。

Then I would use match to test the url, because you can reliably coerce a boolean from the result using !! 然后,我将使用match来测试网址,因为您可以使用!!从结果可靠地强制布尔值!! .

I added a default array option, otherwise it would use the last item iterated, even if no items were matched. 我添加了一个默认的数组选项,否则即使没有匹配的项,它也会使用最后一个迭代的项。 This way you can explicitly tell the script to stop (or otherwise handle the condition) if no items were matched. 这样,如果没有项目匹配,则可以明确地告诉脚本停止(或处理条件)。

Also, some RegEx simplifications. 此外,还进行了一些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];

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

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