简体   繁体   English

如何通过比较JavaScript中的两个arrays的项目来判断是否存在不等项目?

[英]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?如何通过比较两个arrays的项目来判断是否存在不等项并返回true或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您可以通过迭代短数组并检查其所有值来做到这一点,每个 function 都很方便: 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在 chrome 控制台中测试

let freeDrive = ['58:0', '58:1', '58:22'];让 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'];让 filterdDrive = ['58:0', '58:1', '58:2', '58:3', '58:4', '58:5', '58:6', '58:7 '];

checkInclude(freeDrive, filterdDrive); checkInclude(freeDrive, filterdDrive);

false错误的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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