检查两个数组的值是否为 JavaScript 中的 same/equal 的最佳方法
Best way to check if values of two arrays are the same/equal in JavaScript
检查两个数组是否在 JavaScript 中具有 same/equal 值(以任何顺序)的最佳方法是什么?
这些值只是数据库实体的主键,所以它们总是不同的
const result = [1, 3, 8, 77]
const same = [8, 3, 1, 77]
const diff = [8, 3, 5, 77]
areValuesTheSame(result, same) // true
areValuesTheSame(result, diff) // false
areValuesTheSame
方法应该是什么样的?
P.S。这个问题看起来像是重复的,但我没有找到与 Javascript.
相关的任何内容
试试这个:
const result = [1, 3, 8, 77]
const same = [8, 3, 1, 77]
const diff = [8, 3, 5, 77]
const areValuesTheSame = (a,b) => (a.length === b.length) && Object.keys(a.sort()).every(i=>a[i] === b.sort()[i])
console.log(areValuesTheSame(result, same)) // true
console.log(areValuesTheSame(result, diff)) // false
您可以为一个数组计数所有具有 Map
(这是类型保存)的元素,并为另一个数组计数,并检查所有项目的最终计数是否为零。
function haveSameValues(a, b) {
const count = d => (m, v) => m.set(v, (m.get(v) || 0) + d)
return Array
.from(b.reduce(count(-1), a.reduce(count(1), new Map)).values())
.every(v => v === 0);
}
const result = [1, 3, 8, 77]
const same = [8, 3, 1, 77]
const diff = [8, 3, 5, 77]
console.log(haveSameValues(result, same)); // true
console.log(haveSameValues(result, diff)); // false
我做出以下假设:
- 数组只包含数字。
- 你不关心元素的顺序;重新排列数组就可以了。
在这些条件下,我们可以简单地将每个数组转换为规范字符串,方法是对它进行排序并将元素与例如连接起来。一个space。然后(多)集相等归结为简单的字符串相等。
function areValuesTheSame(a, b) {
return a.sort().join(' ') === b.sort().join(' ');
}
const result = [1, 3, 8, 77];
const same = [8, 3, 1, 77];
const diff = [8, 3, 5, 77];
console.log(areValuesTheSame(result, same));
console.log(areValuesTheSame(result, diff));
这可能是最懒/最短的方法。
检查两个数组是否在 JavaScript 中具有 same/equal 值(以任何顺序)的最佳方法是什么?
这些值只是数据库实体的主键,所以它们总是不同的
const result = [1, 3, 8, 77]
const same = [8, 3, 1, 77]
const diff = [8, 3, 5, 77]
areValuesTheSame(result, same) // true
areValuesTheSame(result, diff) // false
areValuesTheSame
方法应该是什么样的?
P.S。这个问题看起来像是重复的,但我没有找到与 Javascript.
相关的任何内容试试这个:
const result = [1, 3, 8, 77]
const same = [8, 3, 1, 77]
const diff = [8, 3, 5, 77]
const areValuesTheSame = (a,b) => (a.length === b.length) && Object.keys(a.sort()).every(i=>a[i] === b.sort()[i])
console.log(areValuesTheSame(result, same)) // true
console.log(areValuesTheSame(result, diff)) // false
您可以为一个数组计数所有具有 Map
(这是类型保存)的元素,并为另一个数组计数,并检查所有项目的最终计数是否为零。
function haveSameValues(a, b) {
const count = d => (m, v) => m.set(v, (m.get(v) || 0) + d)
return Array
.from(b.reduce(count(-1), a.reduce(count(1), new Map)).values())
.every(v => v === 0);
}
const result = [1, 3, 8, 77]
const same = [8, 3, 1, 77]
const diff = [8, 3, 5, 77]
console.log(haveSameValues(result, same)); // true
console.log(haveSameValues(result, diff)); // false
我做出以下假设:
- 数组只包含数字。
- 你不关心元素的顺序;重新排列数组就可以了。
在这些条件下,我们可以简单地将每个数组转换为规范字符串,方法是对它进行排序并将元素与例如连接起来。一个space。然后(多)集相等归结为简单的字符串相等。
function areValuesTheSame(a, b) {
return a.sort().join(' ') === b.sort().join(' ');
}
const result = [1, 3, 8, 77];
const same = [8, 3, 1, 77];
const diff = [8, 3, 5, 77];
console.log(areValuesTheSame(result, same));
console.log(areValuesTheSame(result, diff));
这可能是最懒/最短的方法。