javascript - Javascript 匹配并从数组中查找
问题描述
我有一个超过 2k 的 JSON 数组
const Keyword = [
{
"key": "hello",
"word": "hi. how may i help you?"
},
{
"key": "how are you?",
"word": "I'm good, what about you? "
}
]
我的话
hello , are you available right now?
现在我需要用我的词匹配并找到JSON键你好,如果匹配则返回结果真或假,
我尝试使用下面的代码
const text = "hello , are you available right now?"
if (
Keyword.find((arr) => arr.key.toLowerCase() === text.toLowerCase()) ===
undefined
) {
return false;
} else {
return true;
}
现在的问题是它找到确切的词,但我需要一个解决方案来找到匹配
谢谢
解决方案
您可以在字符串上使用包含方法来检查它是否包含子字符串
const Keyword = [
{
"key": "hello",
"word": "hi. how may i help you?"
},
{
"key": "how are you?",
"word": "I'm good, what about you? "
}
]
const text = "hello , are you available right now?"
const found = Keyword.find(item => text.toLowerCase().includes(item.key.toLowerCase()))
console.log(found)
推荐阅读
- .net - 无法创建更多调度程序。资源耗尽?
- c++ - 使用 OpenGL C++ 渲染到纹理
- c# - 检查器中脚本的设置按钮
- json - 无法运行 powershall 脚本
- html - 使用 goquery 将 html 文本和嵌套标签转换为文字字符串
- python - matplotlib.patches.Arc 并使用 clip_path 参数
- masstransit - 为什么在构造函数中注入的 MassTransit ConsumeContext 不包含 Message?
- android - 我正在使用 Android Studio 中的布局
- c# - “连续快速切多个水果给玩家双倍/三倍积分”
- java - 如何在 Z3 java API 中获取上限和下限?