Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Null通过逻辑OR运算符合并字符串和条件字符串,结果为数字_Javascript_Javascript Objects_Null Coalescing_Logical Or - Fatal编程技术网

Javascript Null通过逻辑OR运算符合并字符串和条件字符串,结果为数字

Javascript Null通过逻辑OR运算符合并字符串和条件字符串,结果为数字,javascript,javascript-objects,null-coalescing,logical-or,Javascript,Javascript Objects,Null Coalescing,Logical Or,通过优先级[NUM_TO_PRIORITY[priorityNum]]对字符串进行ORing(其中priorityNum是输入的),通过条件对象.valuesprorities.includeprioritynum对字符串进行Null合并?priorityNum:PRIORITIES.low应该输出一个字符串,但输出一个与输入匹配的数字。为什么会发生这种情况 这可能是js的一个怪癖,但不确定为什么输出是一个数字,因为测试显示合并应该在两个字符串之间才能输出一个字符串: 常数优先级={ high

通过优先级[NUM_TO_PRIORITY[priorityNum]]对字符串进行ORing(其中priorityNum是输入的),通过条件对象.valuesprorities.includeprioritynum对字符串进行Null合并?priorityNum:PRIORITIES.low应该输出一个字符串,但输出一个与输入匹配的数字。为什么会发生这种情况

这可能是js的一个怪癖,但不确定为什么输出是一个数字,因为测试显示合并应该在两个字符串之间才能输出一个字符串:

常数优先级={ high:‘high’, 低:'低', }; const NUM_TO_优先级={ 0:'高', 1:‘低’, }; const priorityNum=0; console.logPRIORITIES[NUM_TO_PRIORITY[priorityNum]];//高的 console.logObject.valuesprorities.includespriorityNum;//错误的 console.logHIGH | | false//HIGH| |的值高于?,因此代码的计算结果为

(PRIORITIES[NUM_TO_PRIORITY[priorityNum]] || Object.values(PRIORITIES).includes(priorityNum))  ? priorityNum : PRIORITIES.low
常数优先级={ high:‘high’, 低:'低', }; const NUM_TO_优先级={ 0:'高', 1:‘低’, }; const priorityNum=0; console.logPRIORITIES[NUM_TO_PRIORITY[priorityNum]];//高的 console.logObject.valuesprorities.includespriorityNum;//错误的 //HIGH | | false//HIGH| |的值高于?,因此代码的计算结果为

(PRIORITIES[NUM_TO_PRIORITY[priorityNum]] || Object.values(PRIORITIES).includes(priorityNum))  ? priorityNum : PRIORITIES.low
常数优先级={ high:‘high’, 低:'低', }; const NUM_TO_优先级={ 0:'高', 1:‘低’, }; const priorityNum=0; console.logPRIORITIES[NUM_TO_PRIORITY[priorityNum]];//高的 console.logObject.valuesprorities.includespriorityNum;//错误的
//HIGH | | false//HIGH如另一个答案中所述,这是由于运算符优先级

valOne || valTwo ? priorityNum : PRIORITIES.low;
同:

(valOne || valTwo) ? priorityNum : PRIORITIES.low;
但是你想要:

valOne || (valTwo ? priorityNum : PRIORITIES.low);
由于大多数没有记忆20+运算符的优先级,请使用以上括号或更多变量来避免这些错误:

const PRIORITIES = {
    high: 'HIGH',
    low: 'LOW',
};
const NUM_TO_PRIORITY = {
    0: 'high',
    1: 'low',
};
const priorityNum = 0;

const priority = PRIORITIES[NUM_TO_PRIORITY[priorityNum]]
const otherVal = Object.values(PRIORITIES).includes(priorityNum) ? priorityNum : PRIORITIES.low

priority || otherVal; // "HIGH"

正如在另一个答案中提到的,这是由于运算符优先级

valOne || valTwo ? priorityNum : PRIORITIES.low;
同:

(valOne || valTwo) ? priorityNum : PRIORITIES.low;
但是你想要:

valOne || (valTwo ? priorityNum : PRIORITIES.low);
由于大多数没有记忆20+运算符的优先级,请使用以上括号或更多变量来避免这些错误:

const PRIORITIES = {
    high: 'HIGH',
    low: 'LOW',
};
const NUM_TO_PRIORITY = {
    0: 'high',
    1: 'low',
};
const priorityNum = 0;

const priority = PRIORITIES[NUM_TO_PRIORITY[priorityNum]]
const otherVal = Object.values(PRIORITIES).includes(priorityNum) ? priorityNum : PRIORITIES.low

priority || otherVal; // "HIGH"

请单击编辑,然后单击[]代码段编辑器并发布感谢建议,添加代码段以防对其他人有帮助:请单击编辑,然后单击[]代码段编辑器并发布感谢建议,添加代码段以防对其他人有帮助:同意| |具有更高的优先级,但这不意味着它将成为优先级[NUM_TO_PRIORITY[priorityNum]]| | Object.valuesprorities.includespriorityNum哪个解析为高| | | false哪个应该输出高?@surajs02 no哪个解析为更高?priorityNum:PRIORITIES.low并最终解析为0抱歉,没有看到添加括号?在| |之后解决是有意义的,谢谢,会接受答案:同意| |具有更高的优先级,但这不意味着它成为优先级[NUM|u TO|u PRIORITY[priorityNum]]| | Object.valuesprorities.includespriorityNum哪个解析为高| | | false哪个输出高?@surajs02 no哪个解析为更高?priorityNum:PRIORITIES.low并最终解析为0抱歉,没有看到添加括号?在| |之后解决是有意义的,谢谢,会接受答案:因为大多数人没有记忆20+的优先级+1球拍很有用,+1可通过以下方式避免这些问题:因为大多数人没有记忆20+的优先顺序+1球拍很有用,+1可通过以下方式避免这些问题: