如何知道两个数组是否具有相同的值

时间:2011-06-03 15:32:40

标签: javascript arrays compare

我有这两个数组:一个填充了来自ajax请求的信息,另一个存储了用户点击的按钮。我使用这段代码(我填写了样本号):

var array1 = [2, 4];
var array2 = [4, 2]; //It cames from the user button clicks, so it might be disordered.
array1.sort(); //Sorts both Ajax and user info.
array2.sort();
if (array1==array2) {
    doSomething();
}else{
    doAnotherThing();
}

但它总是给false,即使两个数组相同,但名称不同。 (我在Chrome的JS控制台中查看了这个)。那么,有什么方法可以知道这两个数组是否包含相同的数据?为什么要给false?我如何知道第一个数组中的哪些值不在第二个数组中?

27 个答案:

答案 0 :(得分:72)

如果您的数组项不是对象 - 例如,如果它们是数字或字符串,您可以比较它们的连接字符串,看它们是否具有任何顺序的相同成员 -

var array1= [10, 6, 19, 16, 14, 15, 2, 9, 5, 3, 4, 13, 8, 7, 1, 12, 18, 11, 20, 17];
var array2= [12, 18, 20, 11, 19, 14, 6, 7, 8, 16, 9, 3, 1, 13, 5, 4, 15, 10, 2, 17];

if(array1.sort().join(',')=== array2.sort().join(',')){
    alert('same members');
}
else alert('not a match');

答案 1 :(得分:42)

Array.prototype.compare = function(testArr) {
    if (this.length != testArr.length) return false;
    for (var i = 0; i < testArr.length; i++) {
        if (this[i].compare) { //To test values in nested arrays
            if (!this[i].compare(testArr[i])) return false;
        }
        else if (this[i] !== testArr[i]) return false;
    }
    return true;
}

var array1 = [2, 4];
var array2 = [4, 2];
if(array1.sort().compare(array2.sort())) {
    doSomething();
} else {
    doAnotherThing();
}

也许?

答案 2 :(得分:39)

如果您只想检查两个数组是否具有相同的值(无论出现的次数和每个值的顺序),您可以使用lodash来执行此操作:

_.isEmpty(_.xor(array1, array2))

简短,漂亮!

答案 3 :(得分:23)

为什么你的代码不起作用

JavaScript具有primitive data types和非原始数据类型。

对于原始数据类型,=====检查条形图两侧的内容是否具有相同的值。这就是1 === 1为真的原因。

对于非原始数据类型(如数组),=====检查引用相等性。也就是说,他们检查arr1arr2是否是同一个对象。在您的示例中,两个数组具有相同顺序的相同对象,但不等效。

解决方案

当且仅当以下情况时,两个数组arr1arr2具有相同的成员:

  • arr2中的所有内容都在arr1

  • arr1中的所有内容都在arr2

所以这将解决问题(ES2016):

const containsAll = (arr1, arr2) => 
                arr2.every(arr2Item => arr1.includes(arr2Item))

const sameMembers = (arr1, arr2) => 
                        containsAll(arr1, arr2) && containsAll(arr2, arr1);

sameMembers(arr1, arr2); // `true`

使用Underscore的第二个解决方案更接近您的尝试:

arr1.sort();
arr2.sort();

_.isEqual(arr1, arr2); // `true`

它的工作原理是因为isEqual检查“深度相等”,这意味着它不仅仅是参考相等并且比较值。

第三个问题的解决方案

您还询问了如何找出arr1中未包含arr2中的哪些内容。

这样做(ES2015):

const arr1 = [1, 2, 3, 4];
const arr2 = [3, 2, 1];

arr1.filter(arr1Item => !arr2.includes(arr1Item)); // `[4]`

你也可以使用Underscore的difference:方法:

_.difference(arr1, arr2); // `[4]`

更新

请参阅@ Redu的评论 - 我的解决方案适用于sameMembers,但您可能想到的是sameMembersInOrder也称为deepEquals

更新2

如果您不关心数组成员的顺序,ES2015 +的Set可能是比Array更好的数据结构。使用危险的猴子修补程序查看MDN notes on how to implement isSuperset and difference

答案 4 :(得分:14)

function arraysEqual(_arr1, _arr2) {

    if (!Array.isArray(_arr1) || ! Array.isArray(_arr2) || _arr1.length !== _arr2.length)
      return false;

    var arr1 = _arr1.concat().sort();
    var arr2 = _arr2.concat().sort();

    for (var i = 0; i < arr1.length; i++) {

        if (arr1[i] !== arr2[i])
            return false;

    }

    return true;

}

请注意,与先前的答案不同,这不会修改原始数组。

答案 5 :(得分:6)

对象相等性检查:JSON.stringify(array1.sort()) === JSON.stringify(array2.sort())

上述测试也适用于对象数组,在这种情况下使用http://www.w3schools.com/jsref/jsref_sort.asp中记录的排序函数

对于具有平面JSON模式的小数组,可能就足够了。

答案 6 :(得分:6)

那呢?我想是ES 2017:

const array1 = [1, 3, 5];
const array2 = [1, 5, 3];

const isEqual = (array1.length === array2.length) && (array1.every(val => array2.includes(val)));
console.log(isEqual);

第一个条件检查两个数组的长度是否相同,第二个条件检查第一个数组是否为第二个数组的子集。然后,将这两个条件组合在一起,就可以比较两个数组的所有项目,而不必考虑元素的顺序。

上面的代码仅在两个数组都有非重复项的情况下起作用。

答案 7 :(得分:3)

比较这两个数组时,您要比较代表数组的对象,而不是内容。

你必须使用一个函数来比较两者。您可以自己编写一个循环,然后在检查长度是否相同之后将其与另一个进行比较。

答案 8 :(得分:3)

使用ES6进行浅层相等的简单解决方案:

const arr1test = arr1.slice().sort()
const arr2test = arr2.slice().sort()
const equal = !arr1test.some((val, idx) => val !== arr2test[idx])

创建每个数组的浅表副本并对其进行排序。然后使用some()遍历arr1test的值,将arr2test中具有相同索引的值与每个值进行比较。如果所有值都相等,则some()返回false,然后equal的值为true

也可以使用every(),但是它必须循环遍历数组中的每个元素才能满足true的结果,而some()将在找到一个值后立即保释。不相等:

const equal = arr1test.every((val, idx) => val === arr2test[idx])

答案 9 :(得分:3)

您可以使用reduce而不是循环来显得聪明,但是冒着使您的开发人员将您视为智能资产的风险。

function isArrayContentSame(a, b) {
  if (Array.isArray(a) && Array.isArray(b) && a.length == b.length) {
    a = a.concat().sort()
    b = b.concat().sort()
    return a.reduce((acc,e,i) => acc && e === b[i], true)
  } else {
    return false;
  }
}

答案 10 :(得分:2)

我们的目标基本上是检查2个数组是否相等。这里的set表示数学set。渐近最快的排序需要 O(nlog(n))时间。您可以使用字典数据结构渐近地 O(n)时间。在JS中,字典只是具有键和值的对象。

// assumes array elements are primitive types
function areArraysEqualSets(a1, a2) {
  let superSet = {};
  for (let i = 0; i < a1.length; i++) {
    const e = a1[i] + typeof a1[i];
    superSet[e] = 1;
  }

  for (let i = 0; i < a2.length; i++) {
    const e = a2[i] + typeof a2[i];
    if (!superSet[e]) {
      return false;
    }
    superSet[e] = 2;
  }

  for (let e in superSet) {
    if (superSet[e] === 1) {
      return false;
    }
  }

  return true;
}

请注意,此函数适用于基本类型的数组,并假定a1和a2是数组

答案 11 :(得分:1)

  1. 检查两个数组是否具有相同的值但顺序可能不同

function compareArr(arr1, arr2) {
  return arr1.sort().join(',') == arr2.sort().join(',');
}

console.log(compareArr([1,2,3],[1,2,3])); // returns true
console.log(compareArr([1,2,3],[3,2,1])); // returns true
console.log(compareArr([1,2,3],[3,4,1])); // returns false

  1. 检查两个数组是否具有相同的值和相同的顺序(只是不要对其进行排序。)

function compareArrOrder(arr1, arr2) {
  return arr1.join(',') == arr2.join(',');
}

console.log(compareArrOrder([1,2,3],[1,2,3])); // returns true
console.log(compareArrOrder([1,2,3],[3,2,1])); // returns false
console.log(compareArrOrder([1,2,3],[3,4,1])); // returns false

答案 12 :(得分:1)

另一种解决方案:

array1.concat(array2).filter((item, index, currentArr) => currentArr.lastIndexOf(item) == currentArr.indexOf(item)).length == 0;

[...array1, ...array2].filter((item, index, currentArr) => currentArr.lastIndexOf(item) == currentArr.indexOf(item)).length == 0;

答案 13 :(得分:1)

您似乎并不在乎性能,也不确定其他答案中的任何一个都可以。

以下是sameArrayMembers(即[1,2,2] != [1,1,2])和sameArrayMembersAsSet(即[1,2,2] == [1,1,2])的一些基准

如果您声称要检查集合而不是不应该进行长度检查,我可能应该给[1,1,2][1,2]一样。

const tests = {
  'Maciej Krawczyk': (_arr1, _arr2) => {

    if (!Array.isArray(_arr1) || ! Array.isArray(_arr2) || _arr1.length !== _arr2.length)
      return false;

    var arr1 = _arr1.concat().sort();
    var arr2 = _arr2.concat().sort();

    for (var i = 0; i < arr1.length; i++) {

        if (arr1[i] !== arr2[i])
            return false;

    }

    return true;
  },
  'canbax': (a1, a2) => {
    const superSet = {};
    for (const i of a1) {
      const e = i + typeof i;
      superSet[e] = 1;
    }

    for (const i of a2) {
      const e = i + typeof i;
      if (!superSet[e]) {
        return false;
      }
      superSet[e] = 2;
    }

    for (let e in superSet) {
      if (superSet[e] === 1) {
        return false;
      }
    }

    return true;
  },
  'kennebec': (array1, array2) => {
    return array1.slice().sort().join(',')=== array2.slice().sort().join(',');
  },
  'Max Heiber': function() {
    const containsAll = (arr1, arr2) => 
        arr2.every(arr2Item => arr1.includes(arr2Item));
    return (arr1, arr2) => 
        containsAll(arr1, arr2) && containsAll(arr2, arr1);
  }(),
  'gman': (a, b) => {
    if (a.length !== b.length) { return false; }
    const counts = new Map();
    for (const v of a) {
      const count = counts.get(v) || 0;
      counts.set(v, count + 1);
    }
    for (const v of b) {
      const count = counts.get(v);
      if (!count) {   // undefined or 0, both mean arrays are not the same
        return false;
      }
      counts.set(v, count - 1);
    }
    return true;
  },
  'Bemmu': (a, b) => {
    if (Array.isArray(a) && Array.isArray(b) && a.length == b.length) {
      a = a.concat().sort()
      b = b.concat().sort()
      return a.reduce((acc,e,i) => acc && e === b[i], true)
    } else {
      return false;
    }
  },
  'Sandeep': (array1, array2) => {
    return JSON.stringify(array1.sort()) === JSON.stringify(array2.sort());
  },
  'camslice': (arr1, arr2) => {
    const arr1test = arr1.slice().sort();
    const arr2test = arr2.slice().sort();
    return !arr1test.some((val, idx) => val !== arr2test[idx]);
  },
  'Dimitrios Stefos': (arr1, arr2) => {
    if (!Array.isArray(arr1) || !Array.isArray(arr2) || arr1.length!=arr2.length)
        return false;

    return arr1.length==arr1.filter(word => arr2.includes(word)).length;
  },
  'SC1000': (arr1, arr2, opts) => {
    function vKey(i, v) {
      return (opts?.enforceOrder ? `${i}-` : '') + `${typeof v}-${v}`
    }

    if (arr1.length !== arr2.length) return false;

    const d1 = {};
    const d2 = {};  
    for (let i = arr1.length - 1; i >= 0; i--) {
      d1[vKey(i, arr1[i])] = true;
      d2[vKey(i, arr2[i])] = true;
    }

    for (let i = arr1.length - 1; i >= 0; i--) {
      const v = vKey(i, arr1[i]);
      if (d1[v] !== d2[v]) return false;
    }

    for (let i = arr2.length - 1; i >= 0; i--) {
      const v = vKey(i, arr2[i]);
      if (d1[v] !== d2[v]) return false;
    }

    return true
  },
  'Magnus Fohlström': (arr1, arr2) => {
        let count = (arr, val) => arr.reduce((count, curr) => (curr === val ? 1:0) + count, 0);
        return arr1.length === arr2.length && arr1.reduce((checks, val) =>
            checks.concat(count(arr1, val) === count(arr2, val)), []).every(check => check);
   },
};

// ----------------------------

function createExposedPromise() {
 const p = {};
 p.promise = new Promise((resolve, reject) => {
   p.resolve = resolve;
   p.reject = reject;
 });
 return p;
}

function assert(cond) {
  if (!cond) {
    log('assert');
    throw new Error();
  }
}

async function main() {
  await testResults(true, 'sameArrayMembers');
  await testResults(false, 'sameArrayMemembersAsSet');
  
  async function testResults(asSet, msg) {
    log(`\n=======[ ${msg} ] ============`);
    const suite = new Benchmark.Suite();
    let test;

    // reject if they fail simple test
    const a = [1,1,2];
    const b = [1,2,2];
    for (const [name, fn] of Object.entries(tests)) {
      if (fn(a, b) === asSet) {
        log(`${name} fails for ${msg}([${a}], [${b}])`);
      } else {    
        suite.add(name, () => test(fn));
      }
    }

    let endPromise;

    suite.on('cycle', event => log(String(event.target)));
    suite.on('complete', function() {
      log('Fastest is ' + this.filter('fastest').map('name'));
      endPromise.resolve();
    });

    async function runWith(num, title) {
      log(`------[ ${title} ] -----------`);

      const a = [];
      for (let i = 0; i < num; ++i) {
        a[i] = Math.random();
      }
      const b = [...a];
      const c = [...a]; c[c.length / 2 | 0]++;

      endPromise = createExposedPromise();

      test = (fn) => {
        assert(fn(a, b))
        assert(!fn(a, c));
      };

      suite.reset();
      suite.run({'async': true});
      await endPromise.promise;
    }

    await runWith(10, 'small (10)');
    await runWith(100, 'medium (100)');
    await runWith(10000, 'large (10000)');
  }
}
main();

function log(...args) {
  const elem = document.createElement('pre');
  elem.textContent = args.join(' ');
  document.body.appendChild(elem);
}
pre { margin: 0; }
<script src="https://unpkg.com/lodash@4.17.20/lodash.js"></script>
<script src="https://unpkg.com/benchmark@2.1.4/benchmark.js"></script>

由于JS引擎得到更新,结果可能会随着时间而改变。这是2020/09/28的一些结果

Chrome 87

=======[ sameArrayMembers ] ============
canbax fails for sameArrayMembers([1,1,2], [1,2,2])
Max Heiber fails for sameArrayMembers([1,1,2], [1,2,2])
Dimitrios Stefos fails for sameArrayMembers([1,1,2], [1,2,2])
SC1000 fails for sameArrayMembers([1,1,2], [1,2,2])
------[ small (10) ] -----------
Maciej Krawczyk x 246,129 ops/sec ±0.84% (66 runs sampled)
kennebec x 182,234 ops/sec ±0.56% (64 runs sampled)
gman x 377,356 ops/sec ±1.55% (64 runs sampled)
Bemmu x 244,850 ops/sec ±0.76% (64 runs sampled)
Sandeep x 100,529 ops/sec ±1.53% (63 runs sampled)
camslice x 542,577 ops/sec ±0.68% (64 runs sampled)
Fastest is camslice
------[ medium (100) ] -----------
Maciej Krawczyk x 12,121 ops/sec ±1.40% (63 runs sampled)
kennebec x 10,219 ops/sec ±1.60% (63 runs sampled)
gman x 41,225 ops/sec ±1.63% (62 runs sampled)
Bemmu x 12,400 ops/sec ±1.10% (63 runs sampled)
Sandeep x 12,470 ops/sec ±0.50% (64 runs sampled)
camslice x 57,126 ops/sec ±0.54% (64 runs sampled)
Fastest is camslice
------[ large (10000) ] -----------
Maciej Krawczyk x 30.75 ops/sec ±0.86% (42 runs sampled)
kennebec x 27.35 ops/sec ±1.11% (38 runs sampled)
gman x 376 ops/sec ±0.46% (62 runs sampled)
Bemmu x 30.91 ops/sec ±0.77% (42 runs sampled)
Sandeep x 80.33 ops/sec ±0.54% (53 runs sampled)
camslice x 166 ops/sec ±0.44% (61 runs sampled)
Fastest is gman

=======[ sameArrayMemembersAsSet ] ============
Maciej Krawczyk fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
kennebec fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
gman fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
Bemmu fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
Sandeep fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
camslice fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
------[ small (10) ] -----------
canbax x 110,826 ops/sec ±2.07% (63 runs sampled)
Max Heiber x 2,699,807 ops/sec ±1.31% (63 runs sampled)
Dimitrios Stefos x 2,910,096 ops/sec ±0.65% (62 runs sampled)
SC1000 x 59,989 ops/sec ±2.61% (63 runs sampled)
Fastest is Dimitrios Stefos
------[ medium (100) ] -----------
canbax x 9,624 ops/sec ±2.20% (53 runs sampled)
Max Heiber x 88,945 ops/sec ±0.71% (64 runs sampled)
Dimitrios Stefos x 94,425 ops/sec ±0.51% (65 runs sampled)
SC1000 x 5,742 ops/sec ±0.74% (33 runs sampled)
Fastest is Dimitrios Stefos
------[ large (10000) ] -----------
canbax x 59.85 ops/sec ±1.69% (46 runs sampled)
Max Heiber x 13.50 ops/sec ±0.87% (26 runs sampled)
Dimitrios Stefos x 15.40 ops/sec ±0.89% (30 runs sampled)
SC1000 x 37.42 ops/sec ±1.47% (40 runs sampled)
Fastest is canbax

Firefox 80

=======[ sameArrayMembers ] ============
canbax fails for sameArrayMembers([1,1,2], [1,2,2])
Max Heiber fails for sameArrayMembers([1,1,2], [1,2,2])
Dimitrios Stefos fails for sameArrayMembers([1,1,2], [1,2,2])
SC1000 fails for sameArrayMembers([1,1,2], [1,2,2])
------[ small (10) ] -----------
Maciej Krawczyk x 118,391 ops/sec ±0.52% (65 runs sampled)
kennebec x 70,254 ops/sec ±0.40% (67 runs sampled)
gman x 201,659 ops/sec ±3.23% (57 runs sampled)
Bemmu x 118,133 ops/sec ±0.67% (64 runs sampled)
Sandeep x 69,484 ops/sec ±1.40% (65 runs sampled)
camslice x 130,443 ops/sec ±0.55% (65 runs sampled)
Fastest is gman
------[ medium (100) ] -----------
Maciej Krawczyk x 11,418 ops/sec ±2.81% (61 runs sampled)
kennebec x 7,278 ops/sec ±1.37% (41 runs sampled)
gman x 19,748 ops/sec ±6.60% (53 runs sampled)
Bemmu x 11,535 ops/sec ±1.09% (62 runs sampled)
Sandeep x 8,234 ops/sec ±1.46% (45 runs sampled)
camslice x 14,277 ops/sec ±3.08% (60 runs sampled)
Fastest is gman
------[ large (10000) ] -----------
Maciej Krawczyk x 65.25 ops/sec ±2.13% (49 runs sampled)
kennebec x 47.73 ops/sec ±0.82% (51 runs sampled)
gman x 210 ops/sec ±3.54% (54 runs sampled)
Bemmu x 66.90 ops/sec ±0.53% (50 runs sampled)
Sandeep x 63.13 ops/sec ±1.59% (48 runs sampled)
camslice x 115 ops/sec ±1.36% (56 runs sampled)
Fastest is gman

=======[ sameArrayMemembersAsSet ] ============
Maciej Krawczyk fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
kennebec fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
gman fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
Bemmu fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
Sandeep fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
camslice fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
------[ small (10) ] -----------
canbax x 79,433 ops/sec ±1.11% (57 runs sampled)
Max Heiber x 1,822,200 ops/sec ±0.92% (65 runs sampled)
Dimitrios Stefos x 2,258,820 ops/sec ±0.48% (65 runs sampled)
SC1000 x 35,784 ops/sec ±1.42% (63 runs sampled)
Fastest is Dimitrios Stefos
------[ medium (100) ] -----------
canbax x 6,726 ops/sec ±0.60% (38 runs sampled)
Max Heiber x 41,620 ops/sec ±1.08% (65 runs sampled)
Dimitrios Stefos x 53,041 ops/sec ±1.61% (64 runs sampled)
SC1000 x 3,450 ops/sec ±0.56% (64 runs sampled)
Fastest is Dimitrios Stefos
------[ large (10000) ] -----------
canbax x 44.18 ops/sec ±5.87% (42 runs sampled)
Max Heiber x 5.62 ops/sec ±4.79% (19 runs sampled)
Dimitrios Stefos x 6.77 ops/sec ±1.21% (21 runs sampled)
SC1000 x 24.18 ops/sec ±3.50% (36 runs sampled)
Fastest is canbax

Safari 14

=======[ sameArrayMembers ] ============
canbax fails for sameArrayMembers([1,1,2], [1,2,2])
Max Heiber fails for sameArrayMembers([1,1,2], [1,2,2])
Dimitrios Stefos fails for sameArrayMembers([1,1,2], [1,2,2])
SC1000 fails for sameArrayMembers([1,1,2], [1,2,2])
------[ small (10) ] -----------
Maciej Krawczyk x 142,798 ops/sec ±0.50% (65 runs sampled)
kennebec x 118,073 ops/sec ±1.12% (63 runs sampled)
gman x 760,109 ops/sec ±0.46% (66 runs sampled)
Bemmu x 136,265 ops/sec ±0.48% (63 runs sampled)
Sandeep x 69,868 ops/sec ±0.44% (64 runs sampled)
camslice x 155,548 ops/sec ±0.45% (64 runs sampled)
Fastest is gman
------[ medium (100) ] -----------
Maciej Krawczyk x 8,479 ops/sec ±0.52% (46 runs sampled)
kennebec x 5,992 ops/sec ±2.54% (34 runs sampled)
gman x 83,390 ops/sec ±0.37% (64 runs sampled)
Bemmu x 8,615 ops/sec ±0.56% (63 runs sampled)
Sandeep x 5,943 ops/sec ±0.67% (64 runs sampled)
camslice x 8,753 ops/sec ±0.45% (47 runs sampled)
Fastest is gman
------[ large (10000) ] -----------
Maciej Krawczyk x 62.66 ops/sec ±0.87% (51 runs sampled)
kennebec x 46.46 ops/sec ±0.66% (48 runs sampled)
gman x 615 ops/sec ±2.33% (60 runs sampled)
Bemmu x 60.98 ops/sec ±1.28% (52 runs sampled)
Sandeep x 49.11 ops/sec ±2.07% (47 runs sampled)
camslice x 66.33 ops/sec ±4.44% (50 runs sampled)
Fastest is gman

=======[ sameArrayMemembersAsSet ] ============
Maciej Krawczyk fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
kennebec fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
gman fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
Bemmu fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
Sandeep fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
camslice fails for sameArrayMemembersAsSet([1,1,2], [1,2,2])
------[ small (10) ] -----------
canbax x 167,041 ops/sec ±0.63% (64 runs sampled)
Max Heiber x 1,281,054 ops/sec ±0.74% (59 runs sampled)
Dimitrios Stefos x 1,127,639 ops/sec ±0.98% (64 runs sampled)
SC1000 x 112,824 ops/sec ±0.37% (64 runs sampled)
Fastest is Max Heiber
------[ medium (100) ] -----------
canbax x 8,784 ops/sec ±0.53% (47 runs sampled)
Max Heiber x 37,824 ops/sec ±0.52% (65 runs sampled)
Dimitrios Stefos x 41,238 ops/sec ±0.85% (63 runs sampled)
SC1000 x 6,181 ops/sec ±0.61% (35 runs sampled)
Fastest is Dimitrios Stefos
------[ large (10000) ] -----------
canbax x 63.83 ops/sec ±2.46% (49 runs sampled)
Max Heiber x 5.41 ops/sec ±0.52% (18 runs sampled)
Dimitrios Stefos x 6.02 ops/sec ±1.32% (19 runs sampled)
SC1000 x 42.25 ops/sec ±1.45% (45 runs sampled)
Fastest is canbax

答案 14 :(得分:1)

大多数其他解决方案使用排序O(n * log n),使用库或具有O(n ^ 2)复杂性。

这是一个线性复杂度为O(n)的纯Javascript解决方案:

class

测试:

/**
 * Check if two arrays of strings or numbers have the same values
 * @param {string[]|number[]} arr1
 * @param {string[]|number[]} arr2
 * @param {Object} [opts]
 * @param {boolean} [opts.enforceOrder] - By default (false), the order of the values in the arrays doesn't matter.
 * @return {boolean}
 */
function compareArrays(arr1, arr2, opts) {

  function vKey(i, v) {
    return (opts?.enforceOrder ? `${i}-` : '') + `${typeof v}-${v}`
  }

  if (arr1.length !== arr2.length) return false;

  const d1 = {};
  const d2 = {};  
  for (let i = arr1.length - 1; i >= 0; i--) {
    d1[vKey(i, arr1[i])] = true;
    d2[vKey(i, arr2[i])] = true;
  }
  
  for (let i = arr1.length - 1; i >= 0; i--) {
    const v = vKey(i, arr1[i]);
    if (d1[v] !== d2[v]) return false;
  }

  for (let i = arr2.length - 1; i >= 0; i--) {
    const v = vKey(i, arr2[i]);
    if (d1[v] !== d2[v]) return false;
  }

  return true
}

答案 15 :(得分:1)

使用ES6

我们将使用Ramda的equals函数,但可以使用Lodash或Underscore的isEqual

const R = require('ramda');

const arraysHaveSameValues = (arr1, arr2) => R.equals( [...arr1].sort(), [...arr2].sort() )

使用传播运算符,我们避免了对原始数组的变异,并且使函数保持纯净。

答案 16 :(得分:1)

我在游戏项目中有简单的整数值
每个数组中的值数量较少,也需要原始数组不变 所以,我做了以下,它工作得很好。 (编辑代码粘贴在这里)

var sourceArray = [1, 2, 3];
var targetArray = [3, 2, 1];

if (sourceArray.length !== targetArray.length) {
    // not equal
    // did something
    return false;
}

var newSortedSourceArray = sourceArray.slice().sort();
var newSortedTargetArray = targetArray.slice().sort();

if (newSortedSourceArray.toString() !== newSortedTargetArray.toString()) { // MAIN CHECK
    // not equal
    // did something
    return false;
}
else {
    // equal
    // did something
    // continued further below
}

// did some more work

return true;

希望有所帮助。

答案 17 :(得分:1)

如果您使用的是原型框架,则可以使用数组的intersect方法找出它们是否相同(无论顺序如何):

var array1 = [1,2];
var array2 = [2,1];

if(array1.intersect(array2).length === array1.length) {
    alert("arrays are the same!");
}

答案 18 :(得分:0)

经过很长一段时间的回答,但希望这会对寻求简单解决方案和现代新手的人有所帮助。

现在我们可以使用lodashunderscore等多个库来实现这一点。(由于简单,多种功能和高使用率,这些库如今已成为项目的一部分)

您可以使用lodash库中的交集。

_.intersection(['2-1', '1'], ['2-2', '3-1', '2-1']); 
// => ['2-1']

这将适用于任何数据类型。

答案 19 :(得分:0)

如果数组中的项目是基元(数字或单个字符),则可以使用比较长度和使用集合的组合。

function equalArrayItems(arr1, arr2) {
  if (arr1.length !== arr2.length) return false
  const set1 = new Set(arr1)
  const set2 = new Set(arr2)
  const set3 = new Set(arr1, arr2)
  return set1.size === set3.size && set2.size === set3.size
}

答案 20 :(得分:0)

根据接受的答案,我还有另一种方法。

function compareArrays(array1, array2) {

    if (
        !Array.isArray(array1)
        || !Array.isArray(array2)
        || array1.length !== array2.length
    ) return false;

    var first = array1.sort().map(value => (String(value))).join();
    var second = array2.sort().map(value => (String(value))).join();

    return first == second ? true : false;
}

答案 21 :(得分:0)

比较两个数组的功能,以检查两个数组是否具有相同的元素。即使它们出现故障...

对简单数组很有用。 [String,Number,Boolean,null,NaN]。

我不使用.sort(),它修改了原始数组。有人说这很不好...

注意。此函数是有限的,无法比较对象“ [],{}”或这些数组中的函数,数组本身就是对象。

   let arraysHasSameElements = (arr1, arr2) => {
        let count =
            // returns counting of occurrences.
            (arr, val) => arr.reduce((count, curr) => (curr === val ? 1 : 0) + count, 0);

        /* this will return true if lengths of the arrays is equal.
           then compare them.*/
        return arr1.length === arr2.length

            // compare arr1 against arr2.
            && arr1.reduce((checks, val) =>

                /*  creating array of checking if a value has equal amount of occurrences
                    in both arrays, then adds true 'check'. */
                checks.concat(count(arr1, val) === count(arr2, val)), [])

                // checking if each check is equal to true, then .every() returns true.
                .every(check => check);
    }

    let arr1 = ['',-99,true,NaN,21,null,false,'help',-99,'help',NaN], 
        arr2 = [null,-99,'',NaN,NaN,false,true,-99,'help',21,'help'];
    arraysHasSameElements(arr1, arr2); //true

    let arr3 = [false,false,false,false,false,false], 
        arr4 = [false,false,false,false,false,false]
    arraysHasSameElements(arr3, arr4); //true


    // here we have uncommented version.
    let arraysHasSameElements = (arr1, arr2) => {
        let count = (arr, val) => arr.reduce((count, curr) => (curr === val ? 1:0) + count, 0);
        return arr1.length === arr2.length && arr1.reduce((checks, val) =>
            checks.concat(count(arr1, val) === count(arr2, val)), []).every(check => check);
    }

答案 22 :(得分:0)

请检查此答案

var arr1= [12,18];
var arr2= [12, 18, 20, 11, 19, 14, 6, 7, 8, 16, 9, 3, 1, 13, 5, 4, 15, 10, 2, 17];
for(i=0;i<arr1.length;i++)
{
var array1=arr1[i];
for(j=0;j<arr2.length;j++)
{
    var array2=arr2[j];
    if(array1==array2)
    {
return true;
    }
}
}

答案 23 :(得分:0)

如果只想测试原始值,可以尝试:

if(JSON.stringify(arr1.sort()) !== JSON.stringify(arr2.sort())) {
  console.log('invalid');
}

答案 24 :(得分:-1)

如果要比较两个数组,并检查两个数组中是否有相同的对象,它将起作用。 例子:

Array1 = [a,b,c,d]
Array2 = [d,e,f,g]

在这里,“ d”在两个数组中都很常见,因此此函数将返回真值。

  cehckArray(array1, array2) {
    for (let i = 0; i < array1.length; i++) {
      for (let j = 0; j < array2.length; j++) {
        if (array1[i] === array2[j]) {
          return true;
        }
      }
    }
    // Return if no common element exist 
    return false;
  }

答案 25 :(得分:-1)

比较两个数组的简单解决方案:

var array1 = [2, 4];
var array2 = [4, 2];

array1.sort();
array2.sort();

if (array1[0] == array2[0]) {
    console.log("Success");
}else{
    console.log("Wrong");
}

答案 26 :(得分:-2)

这是我没有排序的解决方案:

function isDifferent(t1,t2)
{
    return ((t1.length !== t2.length) || t1.some(tag1 => !t2.some(t=>t===tag1)));
}

let a1=['t','','b','xx','i','p','i'];
let a2=['','o','xx','b','t','p','i'];

console.log("DIFFERENT=",isDifferent(a1,a2)); //false

a1=['a','c','d'];
a2=['a','c'];

console.log("DIFFERENT=",isDifferent(a1,a2)); //true

a1=['a','c','d'];
a2=['a','c','c'];

console.log("DIFFERENT=",isDifferent(a1,a2)); //true

a1=['a','b','c'];
a2=['c','b','a'];

console.log("DIFFERENT=",isDifferent(a1,a2)); //false