如何解决从用于映射的对象数组中设计和生成唯一字符串的最佳方法
我试图解决的问题是如何最好地从一组问题/答案字符串中生成一个唯一的字符串。
假设最终用户填写问卷并回答以下问题:
[
{
"is there a problem": "yes"
},{
"what is it?": "product is damaged"
},{
"do you want a refund?": "yes"
}
]
我需要生成一个表示这些问题和答案的字符串,然后将其映射到 templateId 以向其发送模板化消息。
我想过简单地运行一个 foreach 并将所有问题和回复添加到一起,但问题是如果问题和答案发生变化,或者添加了我们不感兴趣的其他问题以用于发送消息。
我应该使用查找或哈希表类型的查找来代替,以便我只挑选我需要的问题。我也有点担心这里的表现,但这应该没什么大不了的,因为这组问题/答案应该保持相对较小。
我也想知道这个字符串 -> templateId 映射,存储这些信息的最佳方法是什么,一个简单的对象可以正常工作吗?
澄清:
它不必是唯一的或哈希图。基本上我们在后端有电子邮件模板来发送消息。在前端,我们需要根据对问题的一组回复确定要使用的电子邮件模板。所以我的简单解决方案是将问题/答案附加在一起以生成一个字符串,即 "isThereProblemYesWhatisIt?ProductdamagedDoyouwantarefund?Yes"
即后端可用的模板:
productdamaged
productLost
在前端创建的映射。
"isThereProblem?YesWhatisIt?ProductdamagedDoyouwantarefund?Yes" :
"productdamaged"
谢谢
解决方法
从更新后的响应中,您应该查看与您想要完成的非常相似的反应条件渲染。
在前端,您应该将每个问题映射到数组中的一个索引,例如。
["is there a problem","what is it?","do you want a refund?"]
这样你就会知道结果的索引 0 总是“是否有问题”,然后你可以将干净的信息发送到后端,你将能够了解一切的位置:
["yes","product is damaged","yes"]
在您的后端,您将能够运行 switch 语句或 if 语句。这完全取决于您,但看起来像:
if(data[0] === "yes" && data[1] === "product is damaged") {
//return email template 1
}
else if(data[0] === "yes" && data[1] === "product is lost") {
//return email template 2
}
,
通过一小组问题和答案,枚举是可行且可维护的。像这样:
const userSubmission = [{
"is there a problem": "yes"
},{
"what is it?": "product is damaged"
},{
"do you want a refund?": "yes"
}
]
const userSubmission2 = [{
"is there a problem": "yes"
},{
"do you want a refund?": "no"
}
]
const templates = {
"is there a problem-yeswhat is it?-product is damageddo you want a refund?-yes": "templateForDamagedAndRefundWanted","is there a problem-yeswhat is it?-product is damageddo you want a refund?-no": "templateForDamagedAndRefundNotWanted"
}
function keyFromSubmission(submission) {
return submission.reduce((acc,obj) => {
let [key,value] = Object.entries(obj)[0]
acc += `${key}-${value}`
return acc
},"")
}
const key = keyFromSubmission(userSubmission)
console.log(templates[key])
console.log("\nOr,with a different user submission...")
const key2 = keyFromSubmission(userSubmission2)
console.log(templates[key2])
编辑
您可以通过添加间接级别来计划对问题和答案的文本更改。这样,问题、答案及其变体就被象征性地表示出来了。
const questions = [{
questionId: "q1",text: "what is the problem?",answers: [{
answerId: "a1",text: "product was broken"
},{
answerId: "a2",text: "product didn't arrive"
}]
},{
questionId: "q2",text: "do you want a refund?",text: "yes"
},text: "no"
}]
}
]
const userSubmission = [{
"what is the problem?": "product didn't arrive"
},{
"do you want a refund?": "yes"
}
]
function userSubmissionAsSymbols(submission) {
return submission.map(s => {
let [key,value] = Object.entries(s)[0]
let question = questions.find(q => q.text === key)
let answer = question.answers.find(a => a.text === value)
return `${question.questionId}-${answer.answerId}`
})
}
console.log(userSubmissionAsSymbols(userSubmission))
有了这个,你做和以前一样的事情,将从 q/a 值派生的键映射到模板。但是,在此版本中,呈现给用户的文本可以任意更改。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。