设计和从对象数组生成唯一字符串以进行映射的最佳方式
Best way design and generate unique strings from an array of objects for mapping
我要解决的问题是如何最好地从一组 question/answer 个字符串中生成唯一的字符串。
假设最终用户填写了一份调查问卷并回答了以下问题:
[
{
"is there a problem": "yes"
},
{
"what is it?": "product is damaged"
},
{
"do you want a refund?": "yes"
}
]
我需要生成一个表示这些问题和答案的字符串,然后将其映射到 templateId 以将模板消息发送到。
我只是想 运行 一个 foreach 并将所有问题和响应加在一起,但问题是如果问题和答案发生变化或添加了我们对发送消息不感兴趣的其他问题怎么办目的。
我是否应该使用类似查找或哈希表类型的查询来代替,这样我就只挑选出我需要的问题。我也有点担心这里的性能,但这应该没什么大不了的,因为这组 questions/answers 应该保持相对较小。
我也想知道这个字符串 -> templateId 映射,存储此信息的最佳方式是什么,一个简单的对象就可以了?
澄清:
它不必是唯一的或哈希图。基本上我们在后端有电子邮件模板来发送消息。在前端,我们需要根据对问题的回答来确定使用哪个电子邮件模板。所以我的简单解决方案是将 questions/answers 附加在一起以生成一个字符串,即 "isThereProblemYesWhatisIt?ProductDamagedDoyouwantaRefund?Yes"
即后端可用模板:
productDamaged
productLost
在前端创建映射。
"isThereProblem?YesWhatisIt?ProductDamagedDoyouwantaRefund?Yes" :
"productDamaged"
谢谢
根据您更新的回复,您应该看一下 React 条件渲染,它与您想要完成的非常相似。
在您的前端,您应该将每个问题映射到数组中的索引,例如。
["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"
},
{
"what is it?": "product is damaged"
},
{
"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?",
answers: [{
answerId: "a1",
text: "yes"
}, {
answerId: "a2",
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 值派生的键映射到模板。不过,在这个版本中,呈现给用户的文本可以任意更改。
我要解决的问题是如何最好地从一组 question/answer 个字符串中生成唯一的字符串。
假设最终用户填写了一份调查问卷并回答了以下问题:
[
{
"is there a problem": "yes"
},
{
"what is it?": "product is damaged"
},
{
"do you want a refund?": "yes"
}
]
我需要生成一个表示这些问题和答案的字符串,然后将其映射到 templateId 以将模板消息发送到。
我只是想 运行 一个 foreach 并将所有问题和响应加在一起,但问题是如果问题和答案发生变化或添加了我们对发送消息不感兴趣的其他问题怎么办目的。
我是否应该使用类似查找或哈希表类型的查询来代替,这样我就只挑选出我需要的问题。我也有点担心这里的性能,但这应该没什么大不了的,因为这组 questions/answers 应该保持相对较小。
我也想知道这个字符串 -> templateId 映射,存储此信息的最佳方式是什么,一个简单的对象就可以了?
澄清:
它不必是唯一的或哈希图。基本上我们在后端有电子邮件模板来发送消息。在前端,我们需要根据对问题的回答来确定使用哪个电子邮件模板。所以我的简单解决方案是将 questions/answers 附加在一起以生成一个字符串,即 "isThereProblemYesWhatisIt?ProductDamagedDoyouwantaRefund?Yes"
即后端可用模板:
productDamaged
productLost
在前端创建映射。
"isThereProblem?YesWhatisIt?ProductDamagedDoyouwantaRefund?Yes" :
"productDamaged"
谢谢
根据您更新的回复,您应该看一下 React 条件渲染,它与您想要完成的非常相似。
在您的前端,您应该将每个问题映射到数组中的索引,例如。
["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"
},
{
"what is it?": "product is damaged"
},
{
"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?",
answers: [{
answerId: "a1",
text: "yes"
}, {
answerId: "a2",
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 值派生的键映射到模板。不过,在这个版本中,呈现给用户的文本可以任意更改。