一对多对象关系
Posted
技术标签:
【中文标题】一对多对象关系【英文标题】:one to many object relationship 【发布时间】:2020-12-17 01:26:16 【问题描述】:嗨,我有一组对象试图使用 discord 制作聊天机器人,所以在某些情况下,一个输入给我的不仅仅是输出,但我很挣扎,这是我的数组
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"
]
这里的问题是我如何指定当我输入 1 时聊天机器人将响应那个特定的回复,这意味着一个输入有很多输出,所以我该如何做这个
非常感谢。
【问题讨论】:
【参考方案1】:您可以使用Array.find
返回数组中与我们的条件匹配的第一项。
我们可以这样实现
const input = '1'; // we assume the input was '1'
const output = message.find(q =>
return (q.message === input);
);
console.log(output.reply);
来,试试看
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);
【讨论】:
以上是关于一对多对象关系的主要内容,如果未能解决你的问题,请参考以下文章
MyBatis的对象关系映射---一对多N+1策略★★★★★