【问题标题】:Compare two arrays with forEach and add different html elements on condition用 forEach 比较两个数组并根据条件添加不同的 html 元素
【发布时间】:2019-03-23 12:26:53
【问题描述】:

我想用 forEach 比较两个数组。当两个数组有匹配的元素时,它会插入一个 html 元素, 并且,如果没有匹配的元素,它会插入一个不同的 html 元素。

var array1 = [0, 1, 2, 3, 4];
    var array2 = [2, 4];
    
    array1.forEach(function(number1){
      array2.forEach(function(number2){
        if(number1 === number2){
          document.getElementById('numbers').innerHTML += '<span>' + number1 + '</span>'
        } else {
          document.getElementById('numbers').innerHTML += '<span class="inactive">' + number1 + '</span>'
        }
      });
    });
span {
  font-weight: bold;
  margin-left: 10px;
}

.inactive {
  font-weight: normal;
}
&lt;div id='numbers'&gt;&lt;/div&gt;

【问题讨论】:

    标签: javascript html arrays foreach


    【解决方案1】:

    你有两个嵌套循环,这就是你得到 0 0 1 1 2 2 3 3 4 4 的原因。

    一步一步来:

    1. number1 = 0
    2. number2 = 2
    3. 如果条件错误
    4. 输出为 0,类不活动
    5. number1 = 0
    6. number2 = 4
    7. 如果条件错误
    8. 输出为 0,类不活动
    9. number1 = 1
    10. number2 = 2
    11. 如果条件错误
    12. 输出为 1,类不活动
    13. 等等……

    您绝对应该考虑定义一个 numbers 变量,这样您就只能获得该元素一次:

    const numbers = document.getElementById('numbers');
    

    使用includestemplate literals 会让你的代码更优雅:

    array1.forEach(function(number1){
       if (array2.includes(number1)) {
           numbers.innerHTML += `<span> ${number1} </span>`;
       } else {
           numbers.innerHTML += `<span class="inactive"> ${number1} </span>`;
       }
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用Array.includes() 代替第二个Array.forEach() 调用来检查第一个数组中的项目是否在第二个数组中找到:

      const array1 = [0, 1, 2, 3, 4];
      const array2 = [2, 4];
      
      array1.forEach(function(number1){
        console.log(array2.includes(number1) ? 'yes' : 'no');
      });

      或者在你的情况下:

      const array1 = [0, 1, 2, 3, 4];
      const array2 = [2, 4];
      
      const numbers = document.getElementById('numbers');
      
      array1.forEach(function(number1){
        const inactive = !array2.includes(number1) ? 'class="inactive"' : '';
        numbers.innerHTML += `<span ${inactive}> ${number1} </span>`;
      });
      span:not(.inactive) {
        font-weight: bold;
      }
      &lt;div id="numbers"&gt;&lt;/div&gt;

      【讨论】:

      • 谢谢,速度很快。但是,如果不是数字而是像“{number:'0',name:'zero'}”这样的对象并且我想检查数字参数,我该怎么做
      • 您可以改用Array.find(),如果结果是null,您将附加inactive 类。
      • 这将替换第二个示例中的包含行 - const inactive = !array2.find(o =&gt; o.number === number1) ? 'class="inactive"' : ''
      • 谢谢你的作品,但是你需要比较o.numbernumber1.number
      • 确实如此。如果两个数组都包含对象,则需要引用其中的 number 属性。
      【解决方案3】:

      let arrayToHtml = array1.filter(el=>{
          el.includes(array2);
      })
      arrayToHtml.forEach(val=>{
          addHtml(tag,text);
      })
      
      function addHtml(tag, text){
          let newEl = document.body.creatElement(tag);
          newEl.textContent(text);
          document.body.appendChild(newEl)
      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-03
        • 1970-01-01
        • 1970-01-01
        • 2020-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多