Thursday, 29 September 2011


So, array comparison.

Why is there no standard function for this in Javascript?


kikito said...

You can try Array.join() to generate strings. But that will only work on simple, unidimensional arrays.

Other than that, yeah, everyone needs to implement their own comparing function.

But yes, javascript is not high level. People are using languages that compile to it (see Coffeescript).

Ryan said...

How about JSON.stringify()? It should work for arbitrary arrays or objects.

eeeickythump said...

Re not knowing whether Javascript is passing values by reference or by value: Javascript, like most high level languages, does not support pointers. This is Very Good Thing. Forget about pointers.

I have only very basic JS knowledge, but if it's like other high level languages like Lisp, Python etc, then you can assume that all complex data types are passed "by reference". This does not mean any visible pointers are involved, but rather, that passing a value shares that value rather than making a new copy. Values are garbage collected when no references to them remain.

Generally if you need to make a new copy of a value, you call some sort of "copyValue" function, which will return a reference to that new copy.

QBRADQ said...

JavaScript is very much an implementation language, which means you can do something like this: = function(r) { /*Some compare code*/ }

var a = [1, 2, 3];
var b = [1, 2, 3];
if( { /*Some stuff*/ }

I don't really think of a use case for array comparison though. Maybe I'm too old :D

Andrew Doull said...

I've copied and pasted an implementation from somewhere on the web ( but I don't see why everyone should have to do this.

QBRADQ: How do you compare two points to see if they are identical? Or matrices, or anything else which makes sense to refer to using an array... and comparison of object properties is even worse a problem.

Of course, I'm now having further adventures in arrays, wondering why array1.push(array2) isn't working where I want array2 appended as a subarray in the (newly created) final element of array1...

Andrew Doull said...

Trap for young players: If you're going to declare an you need to do so before you create any arrays... pushing works perfectly.

QBRADQ said...

Andrew: Yea, that's not documented anywhere :D Here's how you do it:

var a = [18, "stuff", {prop:3}];
a[a.length] = [1, 2];

I'm not defending JavaScript or anything, it's got it's flaws like every other language. Just trying to share the knowledge :D

Also, comparing two points:

if(a.x == b.x && a.y == b.y)

That's faster than a generic array comparison anyhow. Remember kids, JavaScript arrays are hashes too.

var a = [1, 2];
a.stuffs = 3;

Now a = {
0: 1,
1: 2,
stuffs: 3

Amit said...

Python and C++ have spoiled me. Javascript built-in libraries are so primitive.

Amit said...

BTW, Underscore.js is a useful library to use with Javascript; see (and it has an isEqual function).

Andrew Doull said...

Although I've seen Underscore mentioned a few times, I've not explored it deeply as I was planning on jumping straight to Coffee Script. But neither of two OSes I develop on can run npm (node package manager), so all that is on hold at the moment,