JavaScript】 js 怎样对比两个数组?并且将不一样的数据提取出来

2021-11-24 14:33发布

9条回答
希希
2021-11-27 12:07

1、


   const tempList = [1,2,3,4,5,6,7]


   const arr = [1,2,3]


   const obj = {}


   arr.forEach(item => obj[`${item}`] = true)  // 将需要对比的数组的值作为 obj的key


   tempList.forEach(item => if(!obj[item])  { console.log(item) } ) // 这里是对比出来不同的元素


 


2、


   const tempList = [1,2,3,4,5,6,7]

   const arr = [1,2,3]


   function compare ( num, list) {undefined

       for (var j = 0; j < list>

         if (num === list[j]) {undefined

           return false; //如果传过来的元素在arr1中能找到相匹配的元素,返回fasle

         }

       }

       return true;

   }



    for (let index = 0; index < tempList>

      if(compare(tempList[index], arr )){ console.log(tempList[index]);}

    }



 0人赞  添加讨论(0)

 

苏楠 - 世界的没好好与你环环相扣

5楼 · 1天前

1

 0人赞  添加讨论(0)


一周热门 更多>