0

I want write :

if ( (val = "") **and**if ((val = "") or (val = "") or (val = "")) ), 

then..

 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();}

how i can write "or" opérator ?

charlietfl
  • 170,828
  • 13
  • 121
  • 150
Newbiiiie
  • 1,401
  • 3
  • 14
  • 33
  • 1
    Possible duplicate of [How to write OR in Javascript?](https://stackoverflow.com/questions/12266142/how-to-write-or-in-javascript) – zfrisch Sep 06 '18 at 17:44
  • @zfrisch Ehh, not really. The OP wants to have an OR condition inside the `.includes()` – mhodges Sep 06 '18 at 17:44
  • What is the example input? – epascarello Sep 06 '18 at 17:46
  • 2
    Have you tried to read the [JavaScript documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript)? The [logical OR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Description) operator is described, where else? in the [Logical Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators) section. – axiac Sep 06 '18 at 17:49
  • 1
    And btw, `=` is an [assignment operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Overview), not a [comparison operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators). – axiac Sep 06 '18 at 17:49

1 Answers1

1

One way to do this is to write your own function and provide a string with logical operators in it, like so:

You can parse out each condition and check if the value is in the source array

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

An easier way to do this if you don't care about combining ORs and ANDs (which the above would allow you to do if you extended it with &&) is to simply pass in the individual values as parameters and check those, like so:

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

More specifically for your case:

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();
}
mhodges
  • 10,938
  • 2
  • 28
  • 46
  • @Newbiiiie You're welcome! Glad I was able to help. Check out my revised post for an even easier solution – mhodges Sep 06 '18 at 18:03