js 比较两个数组 提取不同元素

时间:2025-02-06 15:10:47

1、

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

   const arr = [1,2,3]

   const obj = {}

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

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

 

2、

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

   function compare ( num, list) {
       for (var j = 0; j < ; j++) {
         if (num === list[j]) {
           return false; //如果传过来的元素在arr1中能找到相匹配的元素,返回fasle
         }
       }
       return true;
   }


    for (let index = 0; index < ; index++) {
      if(compare(tempList[index], arr )){ (tempList[index]);}
    }