我可以“匹配”吗? 2 个数组中的值,然后使用后续的“匹配”;价值?
我正在尝试实现以下目标,但凭借我的中级 JavaScript 技能,我不确定这是否可行。
这部分与 这个问题。
现在我有 2 个数组
a) 有各种语言(例如“en-GB”、“fr”、“de”等)
b)具有基于上述浏览器语言的URL后缀(例如“fr/”,“de/”,“uk/”)
我想要实现的是:
1)用户点击页面,浏览器检测哪个浏览器它正在使用数组 (a)
2) 根据浏览器基于 (a) 的内容,然后搜索 (b),如果它们匹配,例如,如果语言是“fr”,它将使用 (b) 中数组中的后缀“fr/” .
3) 然后它将将此后缀添加到顶级域(始终保持不变)
这是否有可能实现(我确信是)?可以纯粹通过 JavaScript(或 JQuery)来完成吗?我该怎么做呢?
这是我到目前为止的一些代码:
var IAB_Array = new Array("de-at","nl-be","fr-be","da","de","hu","en-ie","ga","es","fr","it","nl","no","pl","en","en-GB","en-US","en-gb","en-us"); //language array
var IAB_suffix = new Array("at/","be-nl/","be-fr","den/","de/","hu/","ie/","es/","fr/","it/","nl/","nor/","pl/","uk/"); //URL suffix Array
var IAB_lang = "en-GB"; //default language
var IAB_link = "http://www.mysitegoeshere/";
if(navigator.browserLanguage) IAB_lang = navigator.browserLanguage; //change lang if detection supported
if(window.navigator.language) IAB_lang = window.navigator.language; //change lang if detection supported
function IAB_Lang_detect () { //execute search
for (var i=0;i<IAB_Array.length;i++) {
if(IAB_Array[i]==IAB_lang) {
document.write(IAB_Array[i]); //output matched array value
}
}
return false;
}
var IAB_URL = "<a href="+IAB_link+IAB_suffix[1]+">"+IAB_link+IAB_suffix[1]+"</a>"; //this is the resulting URL
document.write(IAB_URL);
IAB_Lang_detect ();
我希望有人可以提供帮助,因为我有点困惑!更重要的是匹配两个数组中的值,然后选择我遇到问题的正确后缀。
谢谢
I'm trying to achieve the following though with my intermediate JavaScript skills I'm not sure if this is possible.
This is related in part to this question.
Now I have 2 arrays
a) Has the various language in (e.g. "en-GB", "fr", "de" etc)
b) Has a suffix of a URL based on the browser language above (e.g. "fr/","de/","uk/")
What I am trying to achieve is:
1) User hits a page, browser detects which browser it is using from the array (a)
2) Depending on what the browser is based on (a), it then searches through (b) and if they match, e.g. if the language is "fr" it will use the suffix "fr/" from the array in (b).
3) It will then add this suffix to a top level domain (which is always constant)
Is this even possible to achieve (I'm sure it is)? Can it be done purely via JavaScript (or JQuery)? How would I go about doing this?
Here's some of the code I have so far:
var IAB_Array = new Array("de-at","nl-be","fr-be","da","de","hu","en-ie","ga","es","fr","it","nl","no","pl","en","en-GB","en-US","en-gb","en-us"); //language array
var IAB_suffix = new Array("at/","be-nl/","be-fr","den/","de/","hu/","ie/","es/","fr/","it/","nl/","nor/","pl/","uk/"); //URL suffix Array
var IAB_lang = "en-GB"; //default language
var IAB_link = "http://www.mysitegoeshere/";
if(navigator.browserLanguage) IAB_lang = navigator.browserLanguage; //change lang if detection supported
if(window.navigator.language) IAB_lang = window.navigator.language; //change lang if detection supported
function IAB_Lang_detect () { //execute search
for (var i=0;i<IAB_Array.length;i++) {
if(IAB_Array[i]==IAB_lang) {
document.write(IAB_Array[i]); //output matched array value
}
}
return false;
}
var IAB_URL = "<a href="+IAB_link+IAB_suffix[1]+">"+IAB_link+IAB_suffix[1]+"</a>"; //this is the resulting URL
document.write(IAB_URL);
IAB_Lang_detect ();
I hope someone can help as I'm a little confused! It's more so the matching the values from the 2 arrays and then subsequently selecting the correct suffix that I'm having trouble with.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会以不同的方式做并使用一个对象:
I'd do it differently and use an object:
我可能根本不会为此使用数组。您可以使用一个对象:
然后直接索引到该对象:
所以:
这是可行的,因为所有 JavaScript 对象都是自由格式的键/值映射。下面是一个更简单的示例:
您可以使用带有文字的点符号或带有字符串的方括号 (
[]
) 符号来查找属性。因此,所有这四个输出完全相同的内容:因此,通过将 IAB 信息制作为对象文字中的映射,您可以更轻松地查找内容:只需使用带有所需语言代码的括号表示法即可。
I probably wouldn't use arrays for this at all. You can use an object:
Then index directly into that object:
So:
This works because all JavaScript objects are free-form key/value maps. Here's a simpler example:
You can look up a property either using dotted notation with a literal, or by using square bracket (
[]
) notation with a string. So all four of these output exactly the same thing:So by making your IAB info a map in an object literal, you can make it much easier to look things up: Just use bracketed notation with the desired language code.