简体   繁体   English

为什么我的带有switch(true)和Array.concat()的代码无法正常工作

[英]Why my code with switch(true) and Array.concat() doesn't work properly

 //JavaScript: function concatArrays() { var recentlyCompleted = ["rc1", "rc2"]; var upcomingActivities = []; var decisionsRequired = []; var records = []; switch(true) { case (recentlyCompleted.length >0): records.concat(recentlyCompleted); console.log("recentlyCompleted.length >0"); console.log(records); case (upcomingActivities.length >0): records.concat(upcomingActivities); console.log("upcomingActivities.length >0"); console.log(records); case (decisionsRequired.length >0): records.concat(decisionsRequired); console.log("decisionsRequired.length >0"); console.log(records); break; } document.getElementById("demo").innerHTML = JSON.stringify(records); var records2 = recentlyCompleted.concat(upcomingActivities).concat(decisionsRequired); document.getElementById("demo2").innerHTML = JSON.stringify(records2); } 
  <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>switch(true)</title> </head> <body> <p>Click the button to join arrays.</p> <button onclick="concatArrays()">Try it</button> <p id="demo"></p> <p id="demo2"></p> </body> 

Sorry please I have read that the main advantage over the obvious if else if sequence is the ability to omit the break statement and execute more than one block. 抱歉,我已经读过,如果不是序列,则相对于显而易见的主要优势是可以忽略break语句并执行多个块。 I wanted to execute more than one block in my case(true) code. 我想在我的case(true)代码中执行多个块。 But I see something wrong with this simple code. 但是我发现这个简单的代码有问题。

I am very sorry but could you explain what's wrong with part of this code with switch(true) Thanks! 非常抱歉,您能否使用switch(true)解释部分代码出了什么问题,谢谢!

Execution more than one block: 执行一个以上的块: 执行一个以上的块

Screenshot of the execution of the correct answer: 正确答案执行的屏幕截图: 在此处输入图片说明

function concatArrays() {
  var recentlyCompleted = ["rc1", "rc2"];
  var upcomingActivities = [];
  var decisionsRequired = [];
  var records = [];
  switch (true) {
    case (recentlyCompleted.length > 0):
      records = records.concat(recentlyCompleted);
      console.log("recentlyCompleted.length >0");
      console.log(records);
    case (upcomingActivities.length > 0):
      records = records.concat(upcomingActivities);
      console.log("upcomingActivities.length >0");
      console.log(records);
    case (decisionsRequired.length > 0):
      records = records.concat(decisionsRequired);
      console.log("decisionsRequired.length >0");
      console.log(records);
      break;
  }
  document.getElementById("demo").innerHTML =
    JSON.stringify(records);

  var records2 = recentlyCompleted
    .concat(upcomingActivities)
    .concat(decisionsRequired);
  document.getElementById("demo2").innerHTML =
    JSON.stringify(records2);
}

A case without a break doesn't re-evaluate the next case for equality, but just runs the block unconditionally - called fallthrough . 一个case没有break不重新评估接下来的case平等,但只是运行块无条件地-所谓的下通 I doubt this is what you wanted. 我怀疑这就是您想要的。 Use multiple successive if statements instead (not an if-else chain) - or just go for the records2 approach if you don't care about the log statements. 而是使用多个连续的if语句(而不是if-else链)-如果您不关心log语句,请直接使用records2方法。

Second, concat creates a new array, it doesn't mutate the target by appending to it. 其次, concat创建一个新数组,它不会通过追加目标来对其进行突变。 See How to append something to an array? 请参见如何向数组追加内容? or How to extend an existing JavaScript array with another array, without creating a new array for various solutions. 如何用另一个数组扩展现有的JavaScript数组,而无需为各种解决方案创建新的数组

You should take a look at the spread operator: 您应该看一下价差运算符:

var records = [...recentlyCompleted, ...upcomingActivities, ..decisionsRequired]

Also your records and records2 are always going to be the same. 此外,您的recordsrecords2总是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM