JavaScript 如何设置条件使 for-loop 和 .include() 过滤正确?

JavaScript how to set conditions to make for-loop and .include() filter correctly?

对如何检查字典中的键是否与其他两个数组中的两个或其中之一的元素匹配感到困惑,然后将匹配的字典条目正确添加到字符串中..

代码:

target = {
  'A': 1,
  'B': 1,
  'D': 2,
  'L': 2
}

let a_include = ["A","B","C","D","E","F","G"];
let b_include = ["H","I","J","K","L","M","N","O","P","Q","R"];

a_string = ""
b_string = ""

if (target.length == 0) {
  a_string += "None for a"
  b_string += "None for b"
} else {
  for(const i of Object.keys(target)) {
  const verify = a_include.includes(i)
  const verify2 = b_include.includes(i)
  if (verify && !verify2) {
    a_string += i + ": " + target[i] + "\n";
    b_string += "None for B";
  } else if (!verify && verify2){
    a_string += "None for A"
    b_string += i + ": " + target[i] + "\n"
  } else {
    a_string += "None for A"
    b_string += "None for B"
   }
 }
}

基本上代码应该检查字典 target 的键是否匹配数组 a_includeb_include 中的任何元素,然后添加到 a_stringb_string(也有字典的键值)

预期功能:

目前如果target = {'A': 1,'B': 1,'D': 2,'L': 2},输出字符串产生:

a_string's output:  "A: 1              b_string's ouput:  "None for BNone for BNone for BL: 2
                     B: 1                                 "
                     D: 2
                     None for A"

但是我想输出的是:

a_string's output:  "A: 1              b_string's ouput:  "L: 2"
                     B: 1                                 
                     D: 2"

我尝试简单地删除“None for A/B”以防止它错误地将“None for A/B”添加到字符串中,但是随后“None for" 如果字典没有与数组之一匹配的键,也不会产生输出:

如果target = {'A': 1,'B': 1,'D': 2},输出字符串产生:

a_string's output:  "A: 1              b_string's ouput:  "None for BNone for BNone for B"
                     B: 1                                 
                     D: 2
                     "

而不是:

a_string's output:  "A: 1              b_string's ouput:  "None for B"
                     B: 1                                 
                     D: 2
                     "

那么如何设置conditions/loop使a和b_string产生预期的输出呢?

*这实际上用于 Discord 嵌入,因此如果删除“None for ...”并将字符串留空,则会出现“MessageEmbed 字段值可能不为空”

感谢阅读。

所以根据我调试你的代码。循环遍历 target 字典的每个 key,条件语句检查每个 key。这就是问题所在,您不希望条件语句检查不必要的条件。例如-

这里,

for(const i of Object.keys(target)) {  
  const verify = a_include.includes(i) 
  const verify2 = b_include.includes(i)  
  if (verify && !verify2) { 
    a_string += i + ": " + target[i] + "\n"; 
    b_string += "None for B"; 
  } else if (!verify && verify2){
    a_string += "None for A"
    b_string += i + ": " + target[i] + "\n"
  } else {
    a_string += "None for A"
    b_string += "None for B"
   }
}

假设,target = { "A": 1, "B": 1, "D": 2, "L": 2 }

循环将检查 A、B、D 和 L 并确认前三个,即 A、B 和 Da_string 但它还会检查 L 和将其分配给 b_string.

问题是 a_string 最后一个键,即 L 是一个恶意键,因此将分配 "None 对于 A",即使 a_string 中已经有一些文本,对于 [=31 也是如此=]b_string前三个键是rouge.

这样您就了解了您的程序是如何工作的,现在寻找解决方案 -

if (verify && !verify2) { 
    a_string += i + ": " + target[i] + "\n"; 
    b_string += "None for B";  // Remove this line
  } else if (!verify && verify2){
    a_string += "None for A"
    b_string += i + ": " + target[i] + "\n" // Remove this line
  } else {
    a_string += "None for A"
    b_string += "None for B"
  }

与其检查那里的恶意键,不如像这样在循环后添加一个新的条件语句 -

{...}
    if (verify && !verify2) {
        a_string += i + ": " + target[i] + "\n";
        // b_string += "None for B";
    } else if (!verify && verify2) {
        // a_string += "None for A"
        b_string += i + ": " + target[i] + "\n"
    } else {
        a_string += "None for A"
        b_string += "None for B"
    }
{...}

//after the loop

if (a_string.length === 0) {
    a_string = "None for a";
} 
if (b_string.length === 0) { // edited
    b_string = "None for b"
}

这是可行的,因为在循环结束后,条件检查是否 a_stringb_string 为空,如果为空,则分配新的更改。

There are more solutions to this particular problem, but I have tried to do it in a simple and understandable way.