2010-06-30 21 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