How to check if a variable is equal to [{}] in Node JS?
Tried all possible ways. But no luck. Help is much appreciated.
Thanks, Varun
How to check if a variable is equal to [{}] in Node JS?
Tried all possible ways. But no luck. Help is much appreciated.
Thanks, Varun
I admit that this is not the most optimal solution, but a quick JSON string compare could work:
var a = [{}]
JSON.stringify(a) == '[{}]' //true
Otherwise, you'll want to implement a more robust object/array comparison such as: Tomáš Zato's answer in How to compare arrays in JavaScript?
Or use a lib such as underscore.js.
If you are comparing you can use deepEqual from assert :
> a =[{}]
[ {} ]
> assert.deepEqual(a,[{}])
undefined
>
This can be used for equivalence in object structures. Almost suitable, the caveat is that it will treat unnested [] and {} same.
> assert.deepEqual(a,[[]])
undefined
> assert.deepEqual({},[])
undefined
Update
deepEqual does not check for strict equality. This however is good for testing if two objects have similar nested structures, barring the above caveat which would apply for leaf nodes in the object. E.g. 2nd case
> assert.deepEqual({a:[{b:[]}]},{a:[{b:[]}]})
undefined
> assert.deepEqual({a:[{b:[]}]},{a:[{b:{}}]})
undefined
> assert.deepEqual({a:[{b:[]}]},{a:[{c:[]}]})
AssertionError: {"a":[{"b":[]}]} deepEqual {"a":[{"c":[]}]}
This comes in handy when checking for object structures.
Oh, it's so simple :
Array.isArray(obj) //is really an array, no array-like object
&& obj.length === 1
&& Object.keys(obj).length === 1 //doesn't have anything enumerable attached
&& typeof obj[0] === 'object' // is an object
&& Object.keys(obj[0]).length === 0 // doesn't have enumerable properties
&& obj[0].__proto__ === Object.prototype // direct ancestor of Object
i recommend against using assert.equal
and assert.deepEqual
—they're doing coercing (==
) comparisons (this is officially justified by the relevant CommonJS specs, but i think they're broken). most of the time, you don't want [ 3 ]
to equal [ '3' ]
, which is what assert.deepEqual
will tell you.
instead, use e.g. https://github.com/jkroso/equals, which (1) works for both primitive values and lists and objects, and (2) lets you check even objects with circular references.
@vkurchatkin i don't find your solution simple—it's quite a bit of code that's tailored for a single, very specific use case. also, typeof
is known to have some gotchas—specifically, it fails for arrays, and __proto__
is a deprecated property (use Object.getPrototypeOf
instead).
testing for equality in JavaScript is quite a recurrent problem that is, sadly, not adequately addressed within NodeJS and other JS VMs (i believe it should be). my recommendation is to use a well-tested library that does both primitive and object equality testing with a single function, preferably one that is robust enough not to fail with circular references.
ideally, you don't want to see anything more complex than equals( d0, d1, d2, ... )
in your application code—anything more is a violation of the Principle of Separation of Concerns.