我想写:
if ( (val = "") **and**if ((val = "") or (val = "") or (val = "")) ),
然后..
if (val.reduce((acc, val) => acc.concat(val), []).includes("CH Nord Mayenne")
&& val.reduce((acc, val) => acc.concat(val), []).includes('Douleur','Psychologie','Nutrition'))
{ CHNM.addTo(myfrugalmap) ;} else {CHNM.remove();}
我怎么写“或”歌剧演员?
答案 0 :(得分:1)
一种实现方法是编写自己的函数并在其中提供带有逻辑运算符的字符串,如下所示:
您可以解析每个条件并检查该值是否在源数组中
var val = [[1,2,"3"], ["4",5,6], [7,8,9]];
var vals = val.reduce((acc, val) => acc.concat(val), []);
function orIncludes(source, testString) {
var orValues = testString.split("||").map(val => {
try {
return JSON.parse(val.trim())
}catch(e) {
return val.trim();
}
});
return orValues.some(val => source.includes(val));
}
console.log(orIncludes(vals, '10 || 12 || 3 || "4"')); // true - matches "4"
console.log(orIncludes(vals, '"3" || 8 || 3 || "4"')); // true - matches "3"
console.log(orIncludes(vals, '"2" || 2 || 5 || "19"')); // true - matches 2
console.log(orIncludes(vals, '10 || 12 || "9" || 4')); // false - no match
一种更简单的方法(如果您不希望将OR和AND组合在一起(如果使用&&
进行扩展,上面的方法可以做到)是简单地传递各个值作为参数并进行检查,例如:
var val = [[1, 2, "3"],["4", 5, 6],[7, 8, 9]];
var vals = val.reduce((acc, val) => acc.concat(val), []);
function orIncludes(source, ...testValues) {
return testValues.some(val => source.includes(val));
}
console.log(orIncludes(vals, 10, 12, 3, "4")); // true - matches "4"
console.log(orIncludes(vals, "3", 8, 3, "4")); // true - matches "3"
console.log(orIncludes(vals, "2", 2, 5, "19")); // true - matches 2
console.log(orIncludes(vals, 10, 12, "9", 4)); // false - no match
更适合您的情况:
var vals = val.reduce((acc, val) => acc.concat(val), []);
if (vals.includes("CH Nord Mayenne") && orIncludes(vals, 'Douleur','Psychologie','Nutrition')){
CHNM.addTo(myfrugalmap);
} else {
CHNM.remove();
}