0

Is there any way to check if an array contains an array with specific values?

Like, I have this array

 drawn[0] = [0,0]

Which I later want to check if still contains [0,0], so I'd do something like

 drawn[0] == [0,0]

But this just returns a false, why? And, more importantly, what should I do instead? Even if I try [0,0] == [0,0] I get a false in return?

Please note that the arrays won't always just be zeros...

Ps. I don't want to use any external libraries, so please keep it to plain ol' javascript

3
  • 2
    Check the array entries? == will compare the object references, not the contents. Commented Feb 22, 2013 at 2:12
  • 1
    possible duplicate of Comparing two arrays in Javascript Commented Feb 22, 2013 at 2:14
  • @FelixKling Oh, I wasn't able to find anything like that... But yea, this is a duplicate then D: Commented Feb 22, 2013 at 2:17

4 Answers 4

4

Everyone's said that you can't compare the arrays because they are objects. That is true. You have several viable solutions including nested loops (either blatantly or abstracted). Others have also suggested this.

A potentially simpler alternative is to compare the the toString values of the two arrays:

drawn[0].toString() == [0,0].toString()

This does require the array contents to be in the same order.

Sign up to request clarification or add additional context in comments.

2 Comments

Since you know what [0,0] will produce as a string, probably better to just compare to 0,0.
@RobG I would assume that [0,0] is a variable in the context in which he's using it. It's also safer to use toString() anyway because you don't have to worry about the parsing of the value. For example, You may think [0, 0].toString() is 0, 0, but it's not. It's 0,0.
3

Arrays in JavaScript are only equal to one another if they're the same object.

You need to do a contents check instead:

if (drawn[0].every(function(item) { return item === 0; })) {
    // all entries are zero.
}

See also: Array.every()

Or in your specific case, simply:

if (drawn[0][0] === 0 && drawn[0][1] === 0) {
    // equal to [0, 0]
}

4 Comments

What is this .all method you speak of? I that standard Javascript? Or pseudo-code perhaps?
@fireeyedboy That should be .every() of course, I have my languages mixed up =p
Worth pointing out that Array.prototype.every is ES5 and therefore not supported by older browsers like IE8.
@RobG The link mentioned in my answer also provides the shim.
2

Check with a for loop, you can't compare with a simple comparison because it compares the references not the values:

[0]    !== [0]    // true
[0][0] ==  [0][0] //true

Comments

0

Objects, including arrays, are compared by object identity. Each time you write [0, 0] you create a new array; so they register as different objects. You would need to iterate yourself through elements and inspect if the elements are the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.