[英]one to many object relationship
hi i have one array of objects trying to make a chat-bot using discord so one input give me more than output under certain circumstances but i'm struggling , here is my array嗨,我有一组对象试图使用不和谐来制作聊天机器人,所以在某些情况下,一个输入给我的不仅仅是输出,但我很挣扎,这是我的数组
let message = [{
"message": "hi",
"reply": "1-order 2-inquiry 3-complain"
},
{
"message": "1",
"reply": "please clarify your address"
},
{
"message": "2",
"reply": "about 1-delivery time 2-delivery fees",
"response": {
"1": "from 8 am to 11 pm",
"2": "10$"
}
},
{
"message": "3",
"reply": "1-expired products 2-other",
"response": {
"1": "please clarify the product name ",
"2": "please leave a comment"
}
}
]
the question here how can i specify that when i enter 1 the chat-bot will respond with that specific reply that means one input has many outputs so how can i make this这里的问题我如何指定当我输入 1 时,聊天机器人将响应该特定回复,这意味着一个输入有多个输出,所以我该如何做到这一点
many thanks.非常感谢。
You can use Array.find
which returns the first item in an array that matches with our condition.您可以使用Array.find
返回与我们的条件匹配的数组中的第一项。 We can implement it like this我们可以这样实现
const input = '1'; // we assume the input was '1'
const output = message.find(q => {
return (q.message === input);
});
console.log(output.reply);
Here, try it来,试试
let message = [ { "message": "hi", "reply": "1-order 2-inquiry 3-complain" }, { "message": "1", "reply": "please clarify your address" }, { "message": "2", "reply": "about 1-delivery time 2-delivery fees", "response": { "1": "from 8 am to 11 pm", "2": "10$" } }, { "message": "3", "reply": "1-expired products 2-other", "response": { "1": "please clarify the product name ", "2": "please leave a comment" } } ]; const input = '1'; // we assume the input was '1' const output = message.find(q => { return (q.message === input); }); console.log(output.reply);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.