Two arrays take the same value

Source array
arr1 = [1, 2, 3 , 4, 5]
modified
arr2 = [3, 4, 5 , 6, 7, 8, 9]

Increased? Deleted?

  • Preoccupation 交集
  const _arr1= new Set(arr1);
  const _arr2 = new Set(arr2);
  const someArr = [...new Set([..._arr1].filter(x => _arr2.has(x)))];

Arr1 array and the array is an array of the same arr2 someArr = [3, 4, 5]

After finding the modifications added and deleted

  • deleted
 const deleteArr = [];
    arr1.map((item) => {
      if (someArr.indexOf(item) === -1) {
        deleteArr.push(item);
      }
    });
    
  • Increased
  arr2.map((item) => {
      if (someArr.indexOf(item) === -1) {
        addArr.push(item);
      }
    });

Published 47 original articles · won praise 42 · Views 140,000 +

Guess you like

Origin blog.csdn.net/zm_miner/article/details/104539609