简体   繁体   English

带有键/值的Javascript测试哈希表

[英]Javascript test hashtable with key / value

I have a saved_tokens hashtable. 我有一个saved_tokens哈希表。 I also have the key and the value. 我也有钥匙和价值。 I would like to check the hashtable to see if the pair exist, if exist, do something. 我想检查哈希表以查看该对是否存在,如果存在,则执行某些操作。 Thanks. 谢谢。

Code thus far 代码到目前为止

var saved_tokens = {}

//method
    _suggest: function( items, clientId ) {

        var filteredItems = [];

        $.each(items, function(i, item ) {
            if(saved_tokens.hasOwnProperty(clientId)) { 

                //need to test for item here
                if(item exist need help) {
                    filteredItems.push(item);
                }
            }
        }); 
   }

I am not really sure what you what, but I assume that you are trying to get the filteredItems list based on the matched items b/w items list + clientID and saved_tokens . 我不确定你是什么,但我假设您正在尝试根据匹配的项目b / w items list + clientIDsaved_tokens获取filteredItems列表。

Try, 尝试,

_suggest: function( items, clientId ) {

        var filteredItems = [];

        $.each(items, function(i, item ) {
            if(saved_tokens.hasOwnProperty(clientId) &&
                         saved_tokens[clientID] === item) {
                    filteredItems.push(item);

                    //add a return false here to terminate the loop.
                }
            }
        }); 
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM