2012-09-11 231 views
1

的兩個數組我有一個字符串以下陣列:如何比較字符串

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

我怎樣才能比較陣列從而使得:

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

回答

3

不能使用關鍵字is(其編譯成===),但你可以添加一個新的is方法原型的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 

然後用它像

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