Jul*_*Lam 894 javascript arrays json
我想比较两个阵列......理想情况下,有效率.没有什么花哨的,只要true它们是相同的,false如果不相同的话.毫不奇怪,比较运算符似乎不起作用.
var a1 = [1,2,3];
var a2 = [1,2,3];
console.log(a1==a2); // Returns false
console.log(JSON.stringify(a1)==JSON.stringify(a2)); // Returns true
Run Code Online (Sandbox Code Playgroud)
每个数组的JSON编码都有,但有没有更快或更"简单"的方法来简单地比较数组而不必迭代每个值?
Tom*_*ica 835
要比较数组,循环遍历它们并比较每个值:
// Warn if overriding existing method
if(Array.prototype.equals)
console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
// if the other array is a falsy value, return
if (!array)
return false;
// compare lengths - can save a lot of time
if (this.length != array.length)
return false;
for (var i = 0, l=this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(array[i]))
return false;
}
else if (this[i] != array[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "equals", {enumerable: false});
Run Code Online (Sandbox Code Playgroud)
[1, 2, [3, 4]].equals([1, 2, [3, 2]]) === false;
[1, "2,3"].equals([1, 2, 3]) === false;
[1, 2, [3, 4]].equals([1, 2, [3, 4]]) === true;
[1, 2, 1, 2].equals([1, 2, 1, 2]) === true;
Run Code Online (Sandbox Code Playgroud)
你可能会说" 但是比较字符串要快得多 - 没有循环...... "那么你应该注意到ARE循环.第一个将Array转换为字符串的递归循环,第二个是比较两个字符串的递归循环.所以这种方法比使用字符串更快.
我相信大量数据应始终存储在数组中,而不是存储在对象中.但是,如果使用对象,也可以对它们进行部分比较.
这是如何做:
我上面已经说过,两个对象实例永远不会相等,即使它们目前包含相同的数据:
({a:1, foo:"bar", numberOfTheBeast: 666}) == ({a:1, foo:"bar", numberOfTheBeast: 666}) //false
Run Code Online (Sandbox Code Playgroud)
这有一个原因,因为可能存在对象内的私有变量.
但是,如果您只是使用对象结构来包含数据,则仍然可以进行比较:
Object.prototype.equals = function(object2) {
//For the first loop, we only check for types
for (propName in this) {
//Check for inherited methods and properties - like .equals itself
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
//Return false if the return value is different
if (this.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
return false;
}
//Check instance type
else if (typeof this[propName] != typeof object2[propName]) {
//Different types => not equal
return false;
}
}
//Now a deeper check using other objects property names
for(propName in object2) {
//We must check instances anyway, there may be a property that only exists in object2
//I wonder, if remembering the checked values from the first loop would be faster or not
if (this.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
return false;
}
else if (typeof this[propName] != typeof object2[propName]) {
return false;
}
//If the property is inherited, do not check any more (it must be equa if both objects inherit it)
if(!this.hasOwnProperty(propName))
continue;
//Now the detail check and recursion
//This returns the script back to the array comparing
/**REQUIRES Array.equals**/
if (this[propName] instanceof Array && object2[propName] instanceof Array) {
// recurse into the nested arrays
if (!this[propName].equals(object2[propName]))
return false;
}
else if (this[propName] instanceof Object && object2[propName] instanceof Object) {
// recurse into another objects
//console.log("Recursing to compare ", this[propName],"with",object2[propName], " both named \""+propName+"\"");
if (!this[propName].equals(object2[propName]))
return false;
}
//Normal value comparison for strings and numbers
else if(this[propName] != object2[propName]) {
return false;
}
}
//If everything passed, let's say YES
return true;
}
Run Code Online (Sandbox Code Playgroud)
但是,请记住,这个用于比较JSON之类的数据,而不是类实例和其他东西.如果你想比较mor复杂的对象,看看这个答案,它是超长函数.
要使这个工作,Array.equals你必须编辑原始功能:
...
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(array[i]))
return false;
}
/**REQUIRES OBJECT COMPARE**/
else if (this[i] instanceof Object && array[i] instanceof Object) {
// recurse into another objects
//console.log("Recursing to compare ", this[propName],"with",object2[propName], " both named \""+propName+"\"");
if (!this[i].equals(array[i]))
return false;
}
else if (this[i] != array[i]) {
...
Run Code Online (Sandbox Code Playgroud)
indexOf和的嵌套数组containsSamy Bencherif为您在嵌套数组中搜索特定对象的情况准备了有用的函数,可在此处获取:https://jsfiddle.net/SamyBencherif/8352y6yw/
小智 348
虽然这只适用于标量数组(见下面的注释),但它很简短:
array1.length === array2.length && array1.every(function(value, index) { return value === array2[index]})
Run Code Online (Sandbox Code Playgroud)
Rr,在带有箭头功能的ECMAScript 6/CoffeeScript/TypeScript中:
array1.length === array2.length && array1.every((value, index) => value === array2[index])
Run Code Online (Sandbox Code Playgroud)
(注意:'标量'在这里表示可以直接使用的值===.因此:数字,字符串,引用对象,引用函数.有关比较运算符的更多信息,请参阅MDN参考).
UPDATE
根据我从评论中读到的内容,对数组进行排序和比较可能会给出准确的结果:
array1.length === array2.length && array1.sort().every(function(value, index) { return value === array2.sort()[index]});
Run Code Online (Sandbox Code Playgroud)
例如:
array1 = [2,3,1,4];
array2 = [1,2,3,4];
Run Code Online (Sandbox Code Playgroud)
然后上面的代码将给出 true
小智 195
我喜欢将Underscore库用于数组/对象重编码项目......在Underscore和Lodash中,无论您是比较数组还是对象,它只是如下所示:
_.isEqual(array1, array2) // returns a boolean
_.isEqual(object1, object2) // returns a boolean
Run Code Online (Sandbox Code Playgroud)
rad*_*tek 101
我认为这是使用JSON stringify执行此操作的最简单方法,在某些情况下它可能是最佳解决方案:
JSON.stringify(a1) === JSON.stringify(a2);
Run Code Online (Sandbox Code Playgroud)
这将对象转换a1和a2使他们能够比较成字符串.在大多数情况下,顺序很重要,因为它可以使用上述答案之一中显示的排序算法对对象进行排序.
请注意,您不再比较对象,而是比较对象的字符串表示形式.它可能不是你想要的.
Tim*_*own 61
目前还不清楚"相同"是什么意思.例如,数组a和b下面是相同的(注意嵌套数组)?
var a = ["foo", ["bar"]], b = ["foo", ["bar"]];
Run Code Online (Sandbox Code Playgroud)
这是一个优化的数组比较函数,它使用严格相等来依次比较每个数组的相应元素,并且不对数据元素本身就是数组进行递归比较,这意味着对于上面的例子,arraysIdentical(a, b)它将返回false.它适用于一般情况,join()基于JSON和解决方案不会:
function arraysIdentical(a, b) {
var i = a.length;
if (i != b.length) return false;
while (i--) {
if (a[i] !== b[i]) return false;
}
return true;
};
Run Code Online (Sandbox Code Playgroud)
Tha*_*you 55
我认为,如果一个特定的实现是"正确的方式",如果它只是"正确"("正确")而不是"错误的"解决方案,那是错误的.Tomáš的解决方案是对基于字符串的数组比较的明显改进,但这并不意味着它客观上是"正确的".什么是对的?它是最快的吗?它最灵活吗?这是最容易理解的吗?它是最快的调试吗?它使用最少的操作吗?它有副作用吗?没有一个解决方案可以拥有所有最好的东西.
Tomáš可以说他的解决方案很快但我也说它不必要地复杂化.它试图成为一个适用于所有阵列的一体化解决方案,嵌套与否.事实上,它甚至不仅仅接受数组作为输入,仍然试图给出"有效"的答案.
我的回答将以不同的方式解决问题.我将从一个通用arrayCompare程序开始,该程序仅涉及单步执行数组.从那里,我们将构建我们的其他基本比较函数,如arrayEqual和arrayDeepEqual等
// arrayCompare :: (a -> a -> Bool) -> [a] -> [a] -> Bool
const arrayCompare = f => ([x,...xs]) => ([y,...ys]) =>
x === undefined && y === undefined
? true
: Boolean (f (x) (y)) && arrayCompare (f) (xs) (ys)
Run Code Online (Sandbox Code Playgroud)
在我看来,最好的代码甚至不需要评论,这也不例外.这里发生的事情很少,你几乎可以毫不费力地理解这个过程的行为.当然,一些ES6语法现在看起来很陌生,但这只是因为ES6相对较新.
如类型所示,arrayCompare需要比较函数f,和两个输入数组,xs以及ys.在大多数情况下,我们所做的只是调用f (x) (y)输入数组中的每个元素.false如果用户定义的f返回,我们会提前返回false- 这要归功于&&短路评估.所以是的,这意味着比较器可以提前停止迭代并防止在不必要时循环通过输入数组的其余部分.
接下来,使用我们的arrayCompare功能,我们可以轻松创建我们可能需要的其他功能.我们将从小学开始arrayEqual......
// equal :: a -> a -> Bool
const equal = x => y =>
x === y // notice: triple equal
// arrayEqual :: [a] -> [a] -> Bool
const arrayEqual =
arrayCompare (equal)
const xs = [1,2,3]
const ys = [1,2,3]
console.log (arrayEqual (xs) (ys)) //=> true
// (1 === 1) && (2 === 2) && (3 === 3) //=> true
const zs = ['1','2','3']
console.log (arrayEqual (xs) (zs)) //=> false
// (1 === '1') //=> false
Run Code Online (Sandbox Code Playgroud)
就那么简单.arrayEqual可以与被定义arrayCompare,并且进行比较的比较功能a,以b使用===(对于全等).
请注意,我们还定义equal了它自己的功能.这突出了arrayCompare作为高阶函数在另一种数据类型(Array)的上下文中利用我们的第一阶比较器的作用.
我们可以arrayLooseEqual使用==替代方法轻松定义.现在比较1(Number)和'1'(String)时,结果将是true......
// looseEqual :: a -> a -> Bool
const looseEqual = x => y =>
x == y // notice: double equal
// arrayLooseEqual :: [a] -> [a] -> Bool
const arrayLooseEqual =
arrayCompare (looseEqual)
const xs = [1,2,3]
const ys = ['1','2','3']
console.log (arrayLooseEqual (xs) (ys)) //=> true
// (1 == '1') && (2 == '2') && (3 == '3') //=> true
Run Code Online (Sandbox Code Playgroud)
你可能已经注意到这只是比较浅的了.当然Tomáš的解决方案是"正确的方式",因为它隐含着深刻的比较,对吗?
好吧,我们的arrayCompare程序功能多样,足以让深度平等测试变得轻而易举......
// isArray :: a -> Bool
const isArray =
Array.isArray
// arrayDeepCompare :: (a -> a -> Bool) -> [a] -> [a] -> Bool
const arrayDeepCompare = f =>
arrayCompare (a => b =>
isArray (a) && isArray (b)
? arrayDeepCompare (f) (a) (b)
: f (a) (b))
const xs = [1,[2,[3]]]
const ys = [1,[2,['3']]]
console.log (arrayDeepCompare (equal) (xs) (ys)) //=> false
// (1 === 1) && (2 === 2) && (3 === '3') //=> false
console.log (arrayDeepCompare (looseEqual) (xs) (ys)) //=> true
// (1 == 1) && (2 == 2) && (3 == '3') //=> true
Run Code Online (Sandbox Code Playgroud)
就那么简单.我们使用另一个高阶函数构建一个深度比较器.这一次,我们包装arrayCompare使用自定义的比较,将检查,如果a和b是数组.如果是,则重新应用arrayDeepCompare否则a与b用户指定的比较器(f)进行比较.这使我们能够将深度比较行为与我们实际比较各个元素的方式分开.即,像上面的例子所示,我们可以深刻的比较使用equal,looseEqual或其他任何比较,我们做.
因为arrayDeepCompare咖喱,我们可以像在前面的例子中那样部分地应用它
// arrayDeepEqual :: [a] -> [a] -> Bool
const arrayDeepEqual =
arrayDeepCompare (equal)
// arrayDeepLooseEqual :: [a] -> [a] -> Bool
const arrayDeepLooseEqual =
arrayDeepCompare (looseEqual)
Run Code Online (Sandbox Code Playgroud)
对我而言,这已经明显改善了Tomáš的解决方案,因为我可以根据需要明确地为我的数组选择浅或深的比较.
现在,如果你有一个对象数组或什么?如果每个对象具有相同的id值,也许你想将这些数组视为"相等" ...
// idEqual :: {id: Number} -> {id: Number} -> Bool
const idEqual = x => y =>
x.id !== undefined && x.id === y.id
// arrayIdEqual :: [a] -> [a] -> Bool
const arrayIdEqual =
arrayCompare (idEqual)
const xs = [{id:1}, {id:2}]
const ys = [{id:1}, {id:2}]
console.log (arrayIdEqual (xs) (ys)) //=> true
// (1 === 1) && (2 === 2) //=> true
const zs = [{id:1}, {id:6}]
console.log (arrayIdEqual (xs) (zs)) //=> false
// (1 === 1) && (2 === 6) //=> false
Run Code Online (Sandbox Code Playgroud)
就那么简单.这里我使用了vanilla JS对象,但是这种类型的比较器可以用于任何对象类型; 甚至你的自定义对象.Tomáš的解决方案需要完全重新设计才能支持这种平等测试
带对象的深度数组?不是问题.我们构建了高度通用的通用函数,因此它们可以在各种用例中使用.
const xs = [{id:1}, [{id:2}]]
const ys = [{id:1}, [{id:2}]]
console.log (arrayCompare (idEqual) (xs) (ys)) //=> false
console.log (arrayDeepCompare (idEqual) (xs) (ys)) //=> true
Run Code Online (Sandbox Code Playgroud)
或者,如果你想做一些其他类型的完全随意的比较呢?也许我想知道每个x是否大于每个y......
// gt :: Number -> Number -> Bool
const gt = x => y =>
x > y
// arrayGt :: [a] -> [a] -> Bool
const arrayGt = arrayCompare (gt)
const xs = [5,10,20]
const ys = [2,4,8]
console.log (arrayGt (xs) (ys)) //=> true
// (5 > 2) && (10 > 4) && (20 > 8) //=> true
const zs = [6,12,24]
console.log (arrayGt (xs) (zs)) //=> false
// (5 > 6) //=> false
Run Code Online (Sandbox Code Playgroud)
你可以看到我们用更少的代码实际做得更多.关于arrayCompare它本身并没有什么复杂的,我们制作的每个自定义比较器都有一个非常简单的实现.
轻松地,我们可以准确地定义我们希望如何比较两个数组 - 浅,深,严格,松散,某些对象属性,或某些任意计算,或这些的任意组合 - 所有这些都使用一个过程,arrayCompare.也许甚至梦想着RegExp比较!我知道孩子们喜欢那些正面的...
它是最快的吗?不.但它可能也不需要.如果速度是用于衡量代码质量的唯一指标,那么很多非常好的代码都会被抛弃 - 这就是我称之为"实用方法"的原因.或者,也许更公平,一个实用方法.这个描述适合这个答案,因为我不是说这个答案只是与其他答案相比是实用的; 这是客观真实的.我们已经获得了很高的实用性,只需很少的代码就很容易推理.没有其他代码可以说我们没有获得此描述.
这是否使它成为您的"正确"解决方案?这取决于你决定.没有人能为你做到这一点; 只有你知道你的需求是什么.几乎在所有情况下,我都认为简单,实用,通用的代码比聪明和快速的类型.你重视的可能会有所不同,所以选择适合你的方法.
我的旧答案更侧重于分解arrayEqual成微小的程序.这是一个有趣的练习,但并不是解决这个问题的最佳(最实际)方法.如果您有兴趣,可以查看此修订历史记录.
uni*_*rio 49
本着原始问题的精神:
我想比较两个阵列......理想情况下,有效率.没有什么花哨的,只要它们是相同的就是真的,如果没有则是假的.
我一直在对这里提出的一些更简单的建议进行性能测试,结果如下(快到慢):
虽然(67%)由蒂姆唐
var i = a1.length;
while (i--) {
if (a1[i] !== a2[i]) return false;
}
return true
Run Code Online (Sandbox Code Playgroud)
a1.every((v,i)=> v === a2[i]);
Run Code Online (Sandbox Code Playgroud)
通过DEI减少(74%)
a1.reduce((a, b) => a && a2.includes(b), true);
Run Code Online (Sandbox Code Playgroud)
加入&toString(78%)由Gaizka Allende和vivek
a1.join('') === a2.join('');
a1.toString() === a2.toString();
Run Code Online (Sandbox Code Playgroud)
Victor Palomo的一半(90%)
a1 == a2.toString();
Run Code Online (Sandbox Code Playgroud)
stringify(100%)来自radtek
JSON.stringify(a1) === JSON.stringify(a2);
Run Code Online (Sandbox Code Playgroud)
请注意,下面的示例假设数组是排序的,一维数组.
.length已经删除了一个共同基准的比较(添加a1.length === a2.length到任何建议,你将获得约10%的性能提升).了解最适合您的解决方案,了解每种解决方案的速度和限制.无关紧要的说明:有趣的是,人们可以通过向下投票按钮获得所有触发快乐的John Waynes对这个问题的完美合理答案.
Eva*_*ner 28
建立TomášZato的答案,我同意只是迭代数组是最快的.另外(就像其他人已经说过的那样),该函数应该被称为equals/equal,而不是比较.鉴于此,我修改了函数以处理比较数组的相似性 - 即它们具有相同的元素,但是无序 - 供个人使用,并且我认为我会把它扔在这里供所有人看.
Array.prototype.equals = function (array, strict) {
if (!array)
return false;
if (arguments.length == 1)
strict = true;
if (this.length != array.length)
return false;
for (var i = 0; i < this.length; i++) {
if (this[i] instanceof Array && array[i] instanceof Array) {
if (!this[i].equals(array[i], strict))
return false;
}
else if (strict && this[i] != array[i]) {
return false;
}
else if (!strict) {
return this.sort().equals(array.sort(), true);
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
此函数采用另一个strict参数,默认为true.此严格参数定义数组是否需要在内容和这些内容的顺序中完全相等,或者只是包含相同的内容.
例:
var arr1 = [1, 2, 3, 4];
var arr2 = [2, 1, 4, 3]; // Loosely equal to 1
var arr3 = [2, 2, 3, 4]; // Not equal to 1
var arr4 = [1, 2, 3, 4]; // Strictly equal to 1
arr1.equals(arr2); // false
arr1.equals(arr2, false); // true
arr1.equals(arr3); // false
arr1.equals(arr3, false); // false
arr1.equals(arr4); // true
arr1.equals(arr4, false); // true
Run Code Online (Sandbox Code Playgroud)
我还写了一个快速的jsfiddle功能和这个例子:http:
//jsfiddle.net/Roundaround/DLkxX/
Jef*_*des 12
虽然这有很多答案,但我相信它会有所帮助:
const newArray = [ ...new Set( [...arr1, ...arr2] ) ]
Run Code Online (Sandbox Code Playgroud)
在问题中没有说明数组的结构如何,所以如果你确定你不会在你的数组中有嵌套数组或对象(它发生在我身上,这就是为什么我来到这里回答)上面的代码会起作用.
会发生什么是我们使用扩展运算符(...)来连接两个数组,然后我们使用Set来消除任何重复.一旦你有了它,你可以比较它们的大小,如果所有三个阵列具有相同的大小,你就可以去.
这个答案也忽略了元素的顺序,正如我所说,确切的情况发生在我身上,所以也许处于相同情况的人可能会在这里结束(就像我一样).
EDIT1.
回答德米特里·格林科的问题:"你为什么在这里使用传播操作员(...) - ...新设置?它不起作用"
考虑以下代码:
const arr1 = [ 'a', 'b' ]
const arr2 = [ 'a', 'b', 'c' ]
const newArray = [ new Set( [...arr1, ...arr2] ) ]
console.log(newArray)
Run Code Online (Sandbox Code Playgroud)
你会得到
[ Set { 'a', 'b', 'c' } ]
Run Code Online (Sandbox Code Playgroud)
为了使用该值,您需要使用一些Set属性(请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).另一方面,当您使用此代码时:
const arr1 = [ 'a', 'b' ]
const arr2 = [ 'a', 'b', 'c' ]
const newArray = [ ...new Set( [...arr1, ...arr2] ) ]
console.log(newArray)
Run Code Online (Sandbox Code Playgroud)
你会得到
[ 'a', 'b', 'c' ]
Run Code Online (Sandbox Code Playgroud)
这就是区别,前者会给我一个Set,它也会起作用,因为我可以得到那个Set的大小,但后者给了我需要的数组,更直接的解决方案.
yes*_*nik 10
在我的例子中,比较数组只包含数字和字符串。此函数将显示数组是否包含相同的元素。
function are_arrs_match(arr1, arr2){
return arr1.sort().toString() === arr2.sort().toString()
}
Run Code Online (Sandbox Code Playgroud)
让我们来测试一下!
arr1 = [1, 2, 3, 'nik']
arr2 = ['nik', 3, 1, 2]
arr3 = [1, 2, 5]
console.log (are_arrs_match(arr1, arr2)) //true
console.log (are_arrs_match(arr1, arr3)) //false
Run Code Online (Sandbox Code Playgroud)
Kam*_*ski 10
对于数字数组,请尝试:
a1==''+a2
Run Code Online (Sandbox Code Playgroud)
a1==''+a2
Run Code Online (Sandbox Code Playgroud)
注意:当数组还包含字符串时,此方法将不起作用,例如a2 = [1, "2,3"].
干得好,
const a = [1, 2, 3]
const b = [1, 2, 3, 4, 5]
const diff = b.filter(e => !a.includes(e))
console.log(diff)Run Code Online (Sandbox Code Playgroud)
上面的大多数答案不适用于无序列表。这也适用于无序列表。
const a = [3, 2, 1]
const b = [1, 2, 3, 4, 5]
const diff = b.filter(e => !a.includes(e))
console.log(diff)Run Code Online (Sandbox Code Playgroud)
如果a的大小大于b,
const a = [1, 2, 3, 4, 5]
const b = [3, 2, 1]
const diff = a.length > b.length ? a.filter(e => !b.includes(e)) : b.filter(e => !a.includes(e))
console.log(diff)Run Code Online (Sandbox Code Playgroud)
这里有很多复杂的长答案,所以我只想贡献一个非常简单的答案:使用 toString() 将数组转换为简单的逗号分隔字符串,您可以轻松地与 === 进行比较
let a = [1, 2, 3]
let b = [1, 2, 3]
let c = [4, 2, 3]
console.log(a.toString()) // this outputs "1,2,3"
console.log(a.toString() === b.toString()) // this outputs true because "1,2,3" === "1,2,3"
console.log(a.toString() === c.toString()) // this outputs false because "1,2,3" != "4,2,3"
Run Code Online (Sandbox Code Playgroud)
这里有很多好的答案。我通常是这样做的——
if ( arr1.length === arr2.length && arr1.every((a1) => arr2.includes(a1)) ) {
// logic
}
Run Code Online (Sandbox Code Playgroud)
every()仅当所有元素都通过给定的比较逻辑时才会返回 true。如果在任何迭代中遇到 false,它都会终止并返回 false。时间复杂度为 O(n*m)。
与JSON.encode相同的行是使用join().
function checkArrays( arrA, arrB ){
//check if lengths are different
if(arrA.length !== arrB.length) return false;
//slice so we do not effect the original
//sort makes sure they are in order
//join makes it a string so we can do a string compare
var cA = arrA.slice().sort().join(",");
var cB = arrB.slice().sort().join(",");
return cA===cB;
}
var a = [1,2,3,4,5];
var b = [5,4,3,2,1];
var c = [1,2,3,4];
var d = [1,2,3,4,6];
var e = ["1","2","3","4","5"]; //will return true
console.log( checkArrays(a,b) ); //true
console.log( checkArrays(a,c) ); //false
console.log( checkArrays(a,d) ); //false
console.log( checkArrays(a,e) ); //true
Run Code Online (Sandbox Code Playgroud)
唯一的问题是如果你关心最后一次比较测试的类型.如果您关心类型,则必须循环.
function checkArrays( arrA, arrB ){
//check if lengths are different
if(arrA.length !== arrB.length) return false;
//slice so we do not effect the orginal
//sort makes sure they are in order
var cA = arrA.slice().sort();
var cB = arrB.slice().sort();
for(var i=0;i<cA.length;i++){
if(cA[i]!==cB[i]) return false;
}
return true;
}
var a = [1,2,3,4,5];
var b = [5,4,3,2,1];
var c = [1,2,3,4];
var d = [1,2,3,4,6];
var e = ["1","2","3","4","5"];
console.log( checkArrays(a,b) ); //true
console.log( checkArrays(a,c) ); //false
console.log( checkArrays(a,d) ); //false
console.log( checkArrays(a,e) ); //false
Run Code Online (Sandbox Code Playgroud)
如果订单应保持相同,而不仅仅是一个循环,则不需要排序.
function checkArrays( arrA, arrB ){
//check if lengths are different
if(arrA.length !== arrB.length) return false;
for(var i=0;i<arrA.length;i++){
if(arrA[i]!==arrB[i]) return false;
}
return true;
}
var a = [1,2,3,4,5];
var b = [5,4,3,2,1];
var c = [1,2,3,4];
var d = [1,2,3,4,6];
var e = ["1","2","3","4","5"];
console.log( checkArrays(a,a) ); //true
console.log( checkArrays(a,b) ); //false
console.log( checkArrays(a,c) ); //false
console.log( checkArrays(a,d) ); //false
console.log( checkArrays(a,e) ); //false
Run Code Online (Sandbox Code Playgroud)
这是打字稿版本:
///sf/answers/1150588281/
export function arraysEqual<T>(a: Array<T>, b: Array<T>): boolean {
if (a === b) return true
if (a == null || b == null) return false
if (a.length != b.length) return false
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false
}
return true
}
///sf/answers/1150588281/
export function arraysDeepEqual<T>(a: Array<T>, b: Array<T>): boolean {
return JSON.stringify(a) === JSON.stringify(b)
}
Run Code Online (Sandbox Code Playgroud)
摩卡咖啡的一些测试案例:
it('arraysEqual', function () {
let a = [1,2]
let b = [1,2]
let c = [2,3]
let d = [2, 3]
let e = ['car','apple','banana']
let f = ['car','apple','banana']
let g = ['car','apple','banan8']
expect(arraysEqual(a, b)).to.equal(true)
expect(arraysEqual(c, d)).to.equal(true)
expect(arraysEqual(a, d)).to.equal(false)
expect(arraysEqual(e, f)).to.equal(true)
expect(arraysEqual(f, g)).to.equal(false)
})
it('arraysDeepEqual', function () {
let a = [1,2]
let b = [1,2]
let c = [2,3]
let d = [2, 3]
let e = ['car','apple','banana']
let f = ['car','apple','banana']
let g = ['car','apple','banan8']
let h = [[1,2],'apple','banan8']
let i = [[1,2],'apple','banan8']
let j = [[1,3],'apple','banan8']
expect(arraysDeepEqual(a, b)).to.equal(true)
expect(arraysDeepEqual(c, d)).to.equal(true)
expect(arraysDeepEqual(a, d)).to.equal(false)
expect(arraysDeepEqual(e, f)).to.equal(true)
expect(arraysDeepEqual(f, g)).to.equal(false)
expect(arraysDeepEqual(h, i)).to.equal(true)
expect(arraysDeepEqual(h, j)).to.equal(false)
})
Run Code Online (Sandbox Code Playgroud)
当两个数组具有相同的元素但顺序不同时,您的代码将无法正确处理这种情况。
用你的例子看看我的代码,它比较了两个元素是数字的数组,你可以修改或扩展它以用于其他元素类型(通过使用 .join() 而不是 .toString())。
var a1 = [1,2,3];
var a2 = [1,2,3];
const arraysAreEqual = a1.sort().toString()==a2.sort().toString();
// true if both arrays have same elements else false
console.log(arraysAreEqual);Run Code Online (Sandbox Code Playgroud)
您可以简单地使用lodash 库中的isEqual。它非常高效和干净。
import {isEqual} from "lodash";
const isTwoArraysEqual = isEqual(array1, array2);
Run Code Online (Sandbox Code Playgroud)
有很多答案显示了如何有效地比较数组。
下面是比较两个 int 或(字符串)数组的最短方法,以代码字节为单位。
const a = [1, 2, 3]
const b = [1, 2, 3]
console.log("1. ", a.join() === b.join())
console.log("2. ", a.join() === [].join())
console.log("3. ", 1 + a === 1 + b)
console.log("4. ", 1 + [] === 1 + b)
// false positives (see flaws)
console.log("5. ", 1 + ["3"] === 1 + [3]) // type differences
console.log("6. ", 1 + ["1,2"] === 1 + ["1", "2"])Run Code Online (Sandbox Code Playgroud)
这是有效的,因为在使用+运算符时,类型会自动转换为允许连接。在这种情况下, the1和 the[1, 2, 3]都被转换为字符串。
在内部,JavaScript 使用[1, 2, 3].join()将数组转换为字符串,然后将它们添加到11,2,3. 在两个数组上执行此操作时,可以简单地使用===或==比较两个字符串。
使用这种技术,比较不关心要比较的数组中的元素是否属于不同类型。由于字符串转换,[1, 2]将等于["1", "2"]。
编辑:正如评论中所指出的,比较字符串数组会产生误报,例如["1,2"]“等于”到["1", "2"]. 如果您确定这些从未发生过(例如在许多代码高尔夫挑战中),则无需担心。
虽然这对代码打高尔夫球很有用,但它可能不应该用于生产代码。指出的两个缺陷也无济于事。
如果它们只是两个数字或字符串数组,那么这是一个快速的单行数组
const array1 = [1, 2, 3];
const array2 = [1, 3, 4];
console.log(array1.join(',') === array2.join(',')) //false
const array3 = [1, 2, 3];
const array4 = [1, 2, 3];
console.log(array3.join(',') === array4.join(',')) //true
Run Code Online (Sandbox Code Playgroud)
有一个在 2020 年推出的Stage 1 提案,允许通过添加Array.prototype.equals到语言中来轻松比较数组。这就是它的工作方式,没有任何库、monkeypatching 或任何其他代码:
[1, 2, 3].equals([1, 2, 3]) // evaluates to true
[1, 2, undefined].equals([1, 2, 3]) // evaluates to false
[1, [2, [3, 4]]].equals([1, [2, [3, 4]]]) // evaluates to true
Run Code Online (Sandbox Code Playgroud)
到目前为止,这只是一个暂定提案 - TC39现在将“花时间检查问题空间、解决方案和跨领域关注点”。如果它进入第 2 阶段,它很有可能最终被整合到正确的语言中。
如果您使用的是像一个测试框架摩卡与柴断言库,你可以使用深平等比较数组.
expect(a1).to.deep.equal(a2)
Run Code Online (Sandbox Code Playgroud)
仅当数组在相应索引处具有相等元素时,才应返回true.
小智 5
另一种方法只需很少的代码(使用Array reduce和Array include):
arr1.length == arr2.length && arr1.reduce((a, b) => a && arr2.includes(b), true)
Run Code Online (Sandbox Code Playgroud)
如果你还想比较顺序的相等性:
arr1.length == arr2.length && arr1.reduce((a, b, i) => a && arr2[i], true)
Run Code Online (Sandbox Code Playgroud)
该length检查确保一个数组中的元素集不仅仅是另一个数组的子集。
减速器用于遍历一个数组并搜索另一数组中的每一项。如果未找到一项,reduce 函数将返回false。
这比较了 2 个未排序的数组:
function areEqual(a, b) {
if ( a.length != b.length) {
return false;
}
return a.filter(function(i) {
return !b.includes(i);
}).length === 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
810780 次 |
| 最近记录: |