简体   繁体   中英

How to find out if there are unequal items by comparing the items of two arrays in JavaScript?

How to find out if there are unequal items by comparing the items of two arrays and return true or false?

let freeDrive = ['58:0', '58:1', '58:22'];
let filterdDrive = ['58:0', '58:1', '58:2', '58:3', '58:4', '58:5', '58:6', '58:7'];

You can do it by iterating on the shorted array and check all its values, the every function is handy for that: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/every

eg

const checkInclude = (arr1, arr2) => {
  if (arr1.length > arr2.length) {
    return checkInclude (arr2, arr1)
  }
  return arr1.every(v => arr2.includes(v))
}

tested in chrome console

let freeDrive = ['58:0', '58:1', '58:22'];

let filterdDrive = ['58:0', '58:1', '58:2', '58:3', '58:4', '58:5', '58:6', '58:7'];

checkInclude(freeDrive, filterdDrive);

false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM