2013-02-09 19 views
0

在下面的代碼中,indexOf()始終返回-1。正在搜索的數組肯定具有其中的值。AS3使用indexOf對數組進行值搜索()

我們已經嘗試將檢查的整數轉換爲字符串,以防數組中有字符串格式,但沒有運氣。

如果任何人都能擺脫任何光明,那將是非常棒的!

AS3

var c:int = 0; 
var storedCachesShared:SharedObject = SharedObject.getLocal("cacheStore"); 
var storedCaches:Array = storedCachesShared.data.cacheArray; 
trace(storedCaches); // 1, 2 


trace(storedCaches.indexOf(c+1)); // Always returns -1 

if(storedCaches.indexOf(c+1) < 0){ 
    storedCaches.push([c+1]); 
    storedCachesShared.flush(); 
} 

非常感謝,尼克

回答

2

這行看起來有問題的。它是將一個數組(如你用[]包圍吧)現有數組的末尾:

storedCaches.push([c+1]); 

我想你的意思是一個int,所以你需要這樣的:

storedCaches.push(c+1); 

indexOf返回-1,因爲它正在尋找一個int,但storedCaches是一個數組Array。

+0

愚蠢的錯誤!感謝負載。 – 2013-02-09 12:10:28