鉴于此:
var results = [
{
"Title": "Battle of Baekgang",
"Space": ["South Korea"]
},
{
"Title": "Victory Tests",
"Space": ["United Kingdom"]
},
{
"Title": "Everett massacre",
"Space": ["United States"]
},
{
"Title": "Bologna massacre",
"Space": ["Italy"]
},
{
"Title": "Milano massacre",
"Space": ["Italy"]
}
];
我如何循环才能说:
If Space value
匹配myNation {do this}
?
设置var countries = ["Italy", United Kingdom", "South Korea"];
我有var regex = new RegExp(countries.join("|"), "i");
我知道我可以做if(location.match(regex)) {
但我需要先存储location
我猜是Space value
还是其他/更好的方式?
更新
我有一个带有多边形的map
,每个多边形都有类似的类:
<path class="italy france germany">
我可以为每个国家/地区设置N
个类名称,json
我会得到一个名为Space
的对象,其中包含一个或多个countries
,所以我' d需要检查json
个国家/地区是否匹配polygons paths
上任意map
中的任何类,如果是,则路径应添加class active
。
答案 0 :(得分:2)
如果我理解你的问题,这应该有效:
var results = [{
"Title": "Battle of Baekgang",
"Space": ["South Korea"]
}, {
"Title": "Victory Tests",
"Space": ["United Kingdom"]
}, {
"Title": "Everett massacre",
"Space": ["United States"]
}, {
"Title": "Bologna massacre",
"Space": ["Italy"]
}, {
"Title": "Milano massacre",
"Space": ["Italy"]
}
];
var countries = ["Italy", "United Kingdom", "South Korea"];
for (var i = 0; i < results.length; i++) {
for (var j = 0; j < results[i]["Space"].length; j++) {
if(countries.includes(results[i]["Space"][j])) {
var matchedCountry = results[i]["Space"][j];
var currentTitle = results[i]["Title"];
//Do other stuff
}
}
}
答案 1 :(得分:2)
results.filter(
({Space})=>countries.includes(Space[0])
).forEach(el=>console.log(el))
只需过滤,然后循环过滤:/
更新:您可以只创建一个空间集:
var spaces = new Set(
results.reduce((acc,el)=>[...acc,...el.Space])
);
然后你可以回顾你的元素:
var els = document.querySelectorAll("path");
els.forEach(function(el){
if(el.className.split(" ").some(clas=>spaces.has(clas))){
//do sth
}
});
答案 2 :(得分:0)
这就是我要去的方式:
var results = [
{
"Title": "Battle of Baekgang",
"Space": ["South Korea"]
},
{
"Title": "Victory Tests",
"Space": ["United Kingdom"]
},
{
"Title": "Everett massacre",
"Space": ["United States"]
},
{
"Title": "Bologna massacre",
"Space": ["Italy"]
},
{
"Title": "Milano massacre",
"Space": ["Italy"]
}
];
var countries = ["Italy"];
for(var i = 0; i < results.length; i++) {
if(countries.includes(results[i]["Space"][0])) { // the [0] is because of Space being an Array
console.log(results[i]["Title"]);
}
}
&#13;
答案 3 :(得分:0)
在ES6解决方案下找到Space
数组中countries
的所有条目
var countries = ["Italy", "United Kingdom", "South Korea"];
var regex = new RegExp(countries.join("|"), "i");
var results = [
{
"Title": "Battle of Baekgang",
"Space": ["South Korea"]
},
{
"Title": "Victory Tests",
"Space": ["United Kingdom"]
},
{
"Title": "Everett massacre",
"Space": ["United States"]
},
{
"Title": "Bologna massacre",
"Space": ["Italy"]
},
{
"Title": "Milano massacre",
"Space": ["Italy"]
}
];
let matches = results.reduce((acc,{Space}) => regex.test(...Space)?acc.concat(...Space):acc, []);
matches.forEach((i) => {
jQuery(`path.${i.toLowerCase()}`).addClass('active')
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<path class="italy france germany">
</path>
<path class="should not be updated">
</path>
&#13;