Comparing ActionScript Arrays

how can I evaluate if my test array is equal to my static constant DEFAULT_ARRAY? should my result be returned true?

public class myClass extends Sprite
{
private static const DEFAULT_ARRAY:Array = new Array(1, 2, 3);

public function myClass()
{
var test:Array = new Array(1, 2, 3);
trace (test == DEFAULT_ARRAY);
}

//traces false
+3
source share
3 answers

Macke has already pointed out the problem. The operator ==will tell you (for reference types such as Array objects) if two variables point to the same object. This is clearly not the case. You have 2 different objects that have the same content.

So what you are probably looking for is a way to compare if 2 arrays have the same content. This seemingly simple task can be more complicated than it seems.

The standard way is to use this function:

function areEqual(a:Array,b:Array):Boolean {
    if(a.length != b.length) {
        return false;
    }
    var len:int = a.length;
    for(var i:int = 0; i < len; i++) {
        if(a[i] !== b[i]) {
            return false;
        }
    }
    return true;
}

(, ) . , ( ). , Numbers ( ints uints), Strings, Boolean, null undefined :

:

var a:int = 0;
var b:int = 0;

:

trace(a == b);

== true, vars :

var a:Object = {data:1};
var b:Object = {data:1};

trace(a == b); // false

var a:Object = {data:1};
var b:Object = a;

trace(a == b); // true

, (, , ), isEqual :

var arr_1:Array = [{data:1}];
var arr_2:Array = [{data:1}];

trace(areEqual(arr_1,arr_2));

? {data:1}, arr_1, {data:1}, arr_2.

, , , , , . , .. I.e. , Γ¬d ( , Γ¬d). , , , .

+7

, mx.utils.ObjectUtil... ActionScript .

ObjectUtil.compare(this.instructions, other.instructions) == 0;
+3

, , , . , . , . 3 3, , . , , , - . , . , , .

, :

private static const DEFAULT_ARRAY:Array = new Array(1, 2, 3);

public function myClass()
{
    var test:Array = DEFAULT_ARRAY;
    trace (test == DEFAULT_ARRAY);
}

//traces true

, , , , . , , :

private static const DEFAULT_ARRAY:Array = new Array(1, 2, 3);

public function myClass()
{
    var test:Array = new Array(1, 2, 3);

    var isEqual:Boolean = true;

    for (var i:int = 0; i < test.length; i++)
    {
        if (test[i] == DEFAULT_ARRAY[i])
            continue;
        else
        {
            isEqual = false;
            break;
        }
    }

    trace (isEqual);
}

//traces true

Hamcrest .

+2
source

Source: https://habr.com/ru/post/1746712/


All Articles