比较两个包含整数的数组 JavaScript

Comparing two arrays containing integers JavaScript

我正在尝试比较两个数组。

firstArray 有 451 个整数,secondArray 有 91 个整数。

secondArray 中的所有整数也在 firstArray 中。

我想从 firstArray 中注销不在 secondArray 中的整数。

我尝试了以下方法:

 for (let i = 0; i < firstArray.length; i++) {
  for (let j = 0; j < secondArray.length; j++) {
     if (firstArray[i] !== secondArray[j]) {
       console.log(firstArray[i])
} } } 

但是它return将 firstArray 的所有 451 个整数编辑为 return 360 个整数(firstArray - secondArray)

第一次在这里发帖,希望够清楚啊。

使用标志来跟踪哪些部分在集合中,哪些不在集合中。

const arrOne = [34,2,12,4,76,7,8,9]
const arrTwo = [8,12,2]
let isInSet = false
const notInSet = []

// Expected output: 34, 4, 76, 7, 9

arrOne.forEach((el, i) => {
  isInSet = false
  arrTwo.forEach((curr, j) => {
    if (el == curr) {
      isInSet = true
    }
  })
  
  if (!isInSet) {
    notInSet.push(el)
  } 
})

console.log(notInSet)
// OR spread them out
console.log(...notInSet)