2010-06-30 24 views
0

我有一個相當大數量的對象「usrSession」,我將它們存儲在我的ArrayCollection usrSessionCollection中。高級搜索/隊列數組收集問題

我正在尋找一個函數,返回最新的userSession添加一個唯一的用戶ID。所以像這樣:

1. 搜索usrSessionCollection並只返回一個userSessions per userID。

2. 當它返回userSessions的X號碼,然後從usrSessionCollection

我被困刪除了他們 - 真的很喜歡,一些代碼,可以幫助我這一點。

function ArrayCollection() { 
    var myArray = new Array; 
    return { 
     empty: function() { 
      myArray.splice(0, myArray.length); 
     }, 
     add: function (myElement) { 
      myArray.push(myElement); 
     } 
    } 
} 

function usrSession(userID, cords, color) { 
    this.UserID = userID; 
    this.Cords = cords; 
    this.Color = color; 
} 

usrSessionCollection = new ArrayCollection(); 

$.getJSON(dataurl, function (data) { 
    for (var x = 0; x < data.length; x++) { 
     usrSessionCollection.add(new usrSession(data[x].usrID.toString(), data[x].usrcords.toString() ,data[x].color.toString()); 
    } 
}); 

謝謝。

回答

2

最大的問題是您已將該數組設置爲外部世界。只有通過陣列可以互動的方法是addempty。爲了能夠搜索數組,您需要在返回的對象中添加該功能,或者公開該數組。下面是一個修改ArrayCollection

function ArrayCollection() { 
    var myArray = new Array; 
    return { 
     empty: function() { 
      myArray.splice(0, myArray.length); 
     }, 
     add: function (myElement) { 
      myArray.push(myElement); 
     }, 
     getAll: function() { 
      return myArray; 
     } 
    } 
} 

我們獲得最後的N個唯一的會話對象usrSessionCollection,向後遍歷數組的會話。保持迄今爲止所看到的所有用戶ID的散列,因此如果重複的用戶ID出現,可以忽略。一旦您收集了N個此類用戶會話或到達了數組的開頭,請返回所有收集的會話。

usrSessionCollection.getLast = function(n) { 
    var sessions = this.getAll(); 
    var uniqueSessions = []; 
    var addedUserIDs = {}, session, count, userID; 

    for(var i = sessions.length - 1; i >= 0, uniqueSessions.length < n; i--) { 
     session = sessions[i]; 
     userID = session.userID; 

     if(!addedUserIDs[userID]) { 
      uniqueSessions.push(session); 
      addedUserIDs[userID] = true; 
     } 
    } 

    return uniqueSessions; 
} 

我不會把刪除步驟和遍歷步驟結合起來,只是爲了保持簡單。所以這裏是從數組中刪除給定會話的remove方法。同樣,最好修改ArrayCollection返回的接口,而不是直接篡改sessions數組。

function ArrayCollection(..) { 
    return { 
     .., 
     remove: function(item) { 
      for(var i = 0; i < myArray.length; i++) { 
       if(item == myArray[i]) { 
        return myArray.splice(i, 1); 
       } 
      } 
      return null; 
     } 
    }; 
} 

例子:獲取最後的10次獨特的會議,並刪除它們:

var sessions = usrSessionCollection.getLast(10); 
for(var i = 0; i < sessions.length; i++) { 
    console.log(sessions[i].UserID); // don't need dummy variable, log directly 
    usrSessionCollection.remove(sessions[i]); 
} 

看到一個working example

+0

因此,當我使用我的會話時,我會這樣做: var sessions = usrSessionCollection.getLast(2); (var i = 0; i 2010-07-01 01:42:45

+0

@ seo20創建一個虛擬會話對象'p'然後立即丟棄它的意義何在?你想在這裏做什麼?您可以直接記錄'sessions [i]'的內容而不使用臨時變量。即使使用臨時變量,也不要不必要地創建'usrSession'對象。相反,直接將'sessions [i]'分配給該臨時變量。例子 - 'var session = sessions [i]; alert(session.UserID);'也更新了答案。 – Anurag 2010-07-01 01:54:57

+0

真棒謝謝了很多 – 2010-07-01 02:02:30

0

你讓你的數組是私有的,所以你不能訪問數據,除了添加一個新元素或全部刪除它們。您需要公開該陣列,或提供公共接口來訪問數據。像first(),next()或item(index)一樣。

然後,您可以將搜索(userID)方法添加到usrSessionCollection,該方法使用此接口遍歷元素並通過用戶ID進行搜索。


UPDATE:這是我會怎麼做: - See it in action。 (點擊預覽)

// user session 
function userSession(userID, cords, color) { 
    this.UserID = userID; 
    this.Cords = cords; 
    this.Color = color; 
} 

// a collection of user sessionions 
// a decorated array basically, with 
// tons of great methods available 
var userSessionCollection = Array; 

userSessionCollection.prototype.lastById = function(userID) { 
    for (var i = this.length; i--;) { 
    if (this[i].UserID === userID) { 
     return this[i]; 
    } 
    } 
    // NOTE: returns undefined by default 
    // which is good. means: no match 
}; 

// we can have aliases for basic functions 
userSessionCollection.prototype.add = Array.prototype.push; 

// or make new ones 
userSessionCollection.prototype.empty = function() { 
    return this.splice(0, this.length); 
}; 

////////////////////////////////////////////////////// 

// make a new collection 
var coll = new userSessionCollection(); 

// put elements in (push and add are also available) 
coll.add (new userSession(134, [112, 443], "#fffff")); 
coll.push(new userSession(23, [32, -32], "#fe233")); 
coll.push(new userSession(324, [1, 53], "#ddddd")); 


// search by id (custom method) 
var search = coll.lastById(134); 
if(search) { 
    console.log(search.UserID); 
} else { 
    console.log("there is no match"); 
} 


// empty and search again 
coll.empty(); 
search = coll.lastById(134); 
if(search) { 
    console.log(search.UserID); 
} else { 
    console.log("there is no match"); 
} 
+0

我希望得到一些更具體的幫助與搜索/刪除數組功能。 – 2010-06-30 23:07:45

+0

你可以通過去搜索一個數組,但是**如果外部數據被隱藏**,並且沒有接口,則不能訪問**。你來自基於類的語言btw?你的代碼似乎有點過於複雜的任務。 – galambalazs 2010-06-30 23:10:34