Reputation: 328
More of a curiosity than anything. I currently have a piece of code that looks like this:
var inputsArray=[];
userInputs.forEach(input=>{
if(isInvert ? query.name == input.name : query.name != input.name){
inputsArray.push(input.id);
}
});
Before that i had:
var inputsArray=[];
userInputs.forEach(input=>{
if(isInvert){
if (query.name == input.name){
inputsArray.push(input.id);
}
}else {
if (query.name != input.name){
inputsArray.push(input.id);
}
}
});
Essentially when isInvert
is true
the opposite values are going to be stored to when isInverse
is false
. I'm wondering if there is an even better way to write the first snippet of code?
Upvotes: 0
Views: 58
Reputation: 2598
you can use this if(isInvert ^ (query.name != input.name)) inputsArray.push(input.id);
wher ^ is the XOR operation
Upvotes: 1
Reputation: 664548
You can further simplify to
if (isInvert == (query.name == input.name)) {
inputsArray.push(input.id);
}
Upvotes: 3