I have to arrays in javascript. Equality comparison on the elements with both === and == succeeds. But when I do a deep equals on the arrays with chai, I keep getting failure.
我必须使用javascript数组。对元素的相等比较,即===和==成功。但是当我在和chai的数组中做一个深度的等号时,我总是失败。
What am I doing wrong? Why is one array showing the element as a string. Clearly it's not actually a string, right? Otherwise the === operator would fail.
我做错了什么?为什么一个数组显示元素为字符串。显然它不是字符串,对吧?否则,==操作符将失败。
The type of the elements in the arrays is mongoose ObjectId.
数组中元素的类型是mongoose ObjectId。
The arrays:
数组:
A: ["57af9c0623a2c3b106efa7a8"]
B: [ 57af9c0623a2c3b106efa7a8 ]
A[0] === B[0] // true
The line which fails:
行失败:
expect(A).to.eql(B);
1 个解决方案
#1
0
This is two equals array but different objects.
这是两个相等的数组但是不同的对象。
var a = ['57af'];
var b = ['57af'];
console.log(a[0] === b[0], a === b);
---
true, false
Use to compare (from here)
用于比较(从这里)
isEqual = a.length == b.length && a.every(function(v,i) { return v === b[i]})
#1
0
This is two equals array but different objects.
这是两个相等的数组但是不同的对象。
var a = ['57af'];
var b = ['57af'];
console.log(a[0] === b[0], a === b);
---
true, false
Use to compare (from here)
用于比较(从这里)
isEqual = a.length == b.length && a.every(function(v,i) { return v === b[i]})