12、数组交集

let nums1 = [1,2,3,2]
let nums2 = [2,2,2]

let arr = [];
for(let i = 0;i<nums1.length;i++){
  let item1 = nums1[i]
  for(let j=0;j<nums2.length;j++){
    let item2 = nums2[j]
    if(item1===item2){
        nums1[i] = null;
        nums2[j] = null;
      arr.push(item1)
      break;
    }
  }
}

console.log(arr)    //[2,2]
let nums1 = [1,2,3,2]
let nums2 = [2,2,2]

let arr = [];
nums1.forEach((item,index)=>{
    let n = nums.indexOf();
    if(n>0){
    nums1.splice(index,1)
    nums2.splice(n,1)
    arr.push(item)
    }
})

results matching ""

    No results matching ""