Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

javascript - 用布尔值反转if条件(Inverting if condition with boolean)

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 .(本质上,当isInverttrue ,相反的值将存储到isInversefalse 。) I'm wondering if there is an even better way to write the first snippet of code?(我想知道是否有更好的方式编写第一段代码?)   ask by Dzyuv001 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can further simplify to(您可以进一步简化为)

if (isInvert == (query.name == input.name)) { inputsArray.push(input.id); }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...