【问题标题】:Advanced search/queue array collection question高级搜索/队列数组集合问题
【发布时间】:2011-03-10 09:34:51
【问题描述】:

我有大量对象“usrSession”,我将它们存储在我的 ArrayCollection usrSessionCollection 中。

我正在寻找一个函数,它返回添加了唯一用户 ID 的最新用户会话。所以是这样的:

1。 搜索 usrSessionCollection,每个 userID 只返回一个 userSessions。

2。 当它返回 x 个 userSessions 然后从 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());
    }
});

谢谢。

【问题讨论】:

    标签: javascript jquery collections queue


    【解决方案1】:

    最大的问题是您将数组设为对外部世界私有。只有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;
            }   
        }
    }
    

    现在要获取usrSessionCollection 中的最后 N 个唯一会话对象,请向后遍历会话数组。维护到目前为止看到的所有用户 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 返回的接口,而不是直接篡改 session 数组。

    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

    【讨论】:

    • 所以当我使用我的会话时,我会这样做: var sessions = usrSessionCollection.getLast(2); for (var i = 0; i
    • @seo20 创建一个虚拟会话对象p,然后立即丢弃它有什么意义?你想在这里做什么?您可以不使用临时变量直接记录sessions[i] 的内容。即使使用临时变量,也不要不必要地创建usrSession 对象。相反,直接将sessions[i] 分配给该临时变量。示例 - var session = sessions[i]; alert(session.UserID); 也更新了答案。
    【解决方案2】:

    您将数组设为私有,因此您无法访问数据,除非添加新元素或将它们全部删除。您需要将数组公开,或提供公共接口来访问数据。比如 first()、next() 或 item(index)。

    然后你可以在usrSessionCollection中添加一个search(userID)方法,它使用这个接口来遍历元素并通过userID进行搜索。


    更新:我会这样做:-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");
    }
    

    【讨论】:

    • 我希望在搜索/从数组中删除功能方面获得更具体的帮助。
    • 你可以通过遍历来搜索数组,但是如果数据被隐藏对于外部并且没有接口,你无法访问它。顺便说一句,您来自基于班级的语言?你的代码似乎有点过于复杂了。
    猜你喜欢
    • 2012-09-12
    • 2016-01-24
    • 2015-03-17
    • 1970-01-01
    • 2021-11-03
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多