如何使用Node.JS将每个键的多个值存储在散列表中? [英] How can I store multiple values per key in a hash table using Node.JS?

查看:175
本文介绍了如何使用Node.JS将每个键的多个值存储在散列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,我有一个键值对的哈希映射,如:

 team,aemt
meat,aemt
car,acr

我想用这样的字符串长度来存储所有的匹配值:

  {4,{team, 肉}} 
{3,{car}

我将如何完成这是什么?

解决方案

按照长度划分你的集合并不是必须的,哈希算法应该能够自己处理。


将每个键的多个值存储在散列表中

p>

你不行。但是,您可以为每个键存储一个字符串数组。

  var words = [team,meat,汽车],
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]];
}


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 accomplish this?

解决方案

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] ];
}

这篇关于如何使用Node.JS将每个键的多个值存储在散列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆