javascript - How can I store multiple values per key in a hash table using Node.JS? - Stack Overflow

In javascript, I have a hash map of key value pairs like:"team", "aemt""meat&

In javascript, I have a hash map of key value pairs like:

"team", "aemt"
"meat", "aemt"
"car", "acr"

And I want to store all of the matching values with the length of the string like this:

{4, {"team","meat"}}
{3, {"car"}

How would I acplish this?

In javascript, I have a hash map of key value pairs like:

"team", "aemt"
"meat", "aemt"
"car", "acr"

And I want to store all of the matching values with the length of the string like this:

{4, {"team","meat"}}
{3, {"car"}

How would I acplish this?

Share Improve this question edited Oct 3, 2013 at 1:46 Nightfirecat 11.6k6 gold badges37 silver badges53 bronze badges asked Oct 3, 2013 at 1:37 Zach LucasZach Lucas 1,6415 gold badges15 silver badges29 bronze badges 14
  • The example you provided is not a valid Object. Please explain in more detail what you are trying to achieve. – Jon Koops Commented Oct 3, 2013 at 1:44
  • This looks like a XY problem. Please tell us what you need that for. Also, please show us the code you've tried, this should be a fairly trivial task. – Bergi Commented Oct 3, 2013 at 1:47
  • basically, I want to store a key value table that has a key (the length of the word), and a value (an array of the words that are anagrams). I want to be able to go through the hash map that I have and add values to this new table – Zach Lucas Commented Oct 3, 2013 at 1:49
  • Ah I see, you're storing the word and an anagram of the word. Is that correct? – Jon Koops Commented Oct 3, 2013 at 1:49
  • I'm storing the word, and the alphabetically sorted version of that word – Zach Lucas Commented Oct 3, 2013 at 1:50
 |  Show 9 more ments

1 Answer 1

Reset to default 2

Dividing your sets by length should not be necessary, the hashing algorithm should be able to take care of that itself. It would only be advisable if you're going to sort your words by length.

store multiple values per key in a hash table

You cannot. However, you can store an array of strings for each key.

var words = ["team", "meat", "car"],
    map = {};
for (var i=0; i<words.length; i++) {
    var key = words[i].toLowercase().split('').sort().join('');
    if (key in map)
        map[key].push(words[i]);
    else
        map[key] = [ words[i] ];
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744391605a4571930.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信