javascript - 如何使用 Javascript 快速搜索 JSON?
问题描述
我有一个词形还原嵌套字典,比如
{
{"abbreviate": ["abbreviated","abbreviates","abbreviating"]},
{"abdicate": ["abdicated","abdicates","abdicating"]}
}
我想快速搜索值以获取相应的键(查找abbreviated
→输出abbreviate
)。Fast
- 因为文件大约 6MB。这个搜索将在 Chrome 扩展中使用,所以我更喜欢 Javascript,而不是 Python。
完成这种搜索的技术是什么?
解决方案
在我看来,您需要将对象转换为用于快速哈希搜索的表单:
{
abbreviated: 'abbreviate',
abbreviates: 'abbreviate',
abbreviating: 'abbreviate',
abdicated: 'abdicate',
abdicates: 'abdicate',
abdicating: 'abdicate',
};
const data = {
abbreviate: ['abbreviated', 'abbreviates', 'abbreviating'],
abdicate: ['abdicated', 'abdicates', 'abdicating'],
};
const dictionary = Object.keys(data).reduce((dict, key) => {
const records = data[key];
const obj = records.reduce((acc, val) => ({ ...acc, [val]: key }), {});
return { ...dict, ...obj };
}, {});
console.log(dictionary['abbreviated']);
推荐阅读
- spring-integration - 使用 @Valid 将 JSON 集成到对象
- asp.net-core - .Net Core 操作的唯一 ID
- node.js - Gulp 错误 node_contextify - 断言 'args[1]'->IsString() 失败
- r - 使用R将大量文件组织到文件夹中?
- oauth-2.0 - 如何正确授权嵌套的微服务调用
- javascript - Vue 未定义/不是构造函数
- flutter - 我不明白为什么不从小部件树中删除可解雇
- javascript - 如何添加与传递的道具编号一样多的按钮,而不会在 React 上导致过多的重新渲染?
- python - 如何编写我的 Dockerfile 以包含 chromedriver?
- python - 我想从抓取的工作列表中查看前 50 个常用词。我不断获得所有词频,包括频率为 1 的词