【问题标题】:How to find the common elements in 2 arrays in Javascript with no duplicates (unless there are 2 of the same element)?如何在没有重复的Javascript中找到2个数组中的公共元素(除非有2个相同的元素)?
【发布时间】:2022-09-24 12:29:52
【问题描述】:

例如,如果我有 2 个数组分别是 [\'dog\', \'cat\', \'zebra\', \'lion\', \'cat\'][\'dog\', \'cat\', \'cat\', \'frog\'],那么应该创建一个只包含 [\'dog\', \'cat\', \'cat\'] 的新数组。

    标签: javascript arrays


    【解决方案1】:

    您可以使用两个循环,并将它们相互比较。 前任。

    for(let i=0; i<ar1.length; i++)
     {
         for(let j=0; j<ar2.length; j++){
           if(ar1[i]==ar[2])
               res.push(ar[i]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-08
      • 2021-09-21
      • 2014-04-05
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      相关资源
      最近更新 更多