Running Turtle
Running Turtle

Reputation: 12752

how to compare two arrays of strings

I have the following arrays of strings:

array1 = ["a", "b", "c"]
array2 = ["a", "c", "b"]
array3 = ["a", "b"]
array4 = ["a", "b", "c"]

How can I compare the arrays so that:

array1 is array2 #false
array1 is array3 #false
array1 is array4 #true

Upvotes: 1

Views: 688

Answers (1)

Christoph Leiter
Christoph Leiter

Reputation: 9345

You can't use the keyword is (which compiles to ===), but you can add a new is method to the prototype of Array:

Array::is = (o) ->
  return true if this is o
  return false if this.length isnt o.length
  for i in [0..this.length]
    return false if this[i] isnt o[i]
  true

Then use it like

array1 = ["a", "b", "c"]
array2 = ["a", "c", "b"]
array3 = ["a", "b"]
array4 = ["a", "b", "c"]

alert array1.is array2
alert array1.is array3
alert array1.is array4

Upvotes: 3

Related Questions