0

我要解决的问题是如何最好地从一组问题/答案字符串中生成唯一的字符串。

假设最终用户填写问卷并回答以下问题:

   [
       {
           "is there a problem": "yes"
       },
       {
           "what is it?": "product is damaged"
       },
       {
           "do you want a refund?": "yes"
       }
   ]

我需要生成一个代表这些问题和答案的字符串,然后将其映射到 templateId 以发送模板化消息。

我考虑过简单地运行一个 foreach 并将所有问题和响应添加在一起,但问题是如果问题和答案发生变化或添加了我们不感兴趣的其他问题以用于发送消息目的。

我应该使用 find 或 hashtable 类型的查找来代替,所以我只选择我需要的问题。我也有点担心这里的表现,但这应该没什么大不了的,因为这组问题/答案应该保持相对较小。

我也想知道这个字符串 - > templateId 映射,存储这些信息的最佳方式是什么,一个简单的对象可以正常工作吗?

澄清:

它不必是唯一的或哈希图。基本上我们在后端有电子邮件模板来发送消息。在前端,我们需要根据对问题的一组回答来确定使用哪个电子邮件模板。所以我的简单解决方案是将问题/答案附加在一起以生成一个字符串,即"isThereProblemYesWhatisIt?ProductDamagedDoyouwantaRefund?Yes"

即后端可用的模板:

productDamaged
productLost

在前端创建的映射。

"isThereProblem?YesWhatisIt?ProductDamagedDoyouwantaRefund?Yes" : 
"productDamaged"

谢谢

4

2 回答 2

0

从您更新的响应中,您应该查看与您想要完成的非常相似的反应条件渲染。

在您的前端,您应该将每个问题映射到数组中的索引,例如。

["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
}
于 2021-06-12T23:01:13.457 回答
0

通过一小组问题和答案,枚举是可行和可维护的。像这样的东西:

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 值派生的键映射到模板。但是,在这个版本中,呈现给用户的文本可以任意更改。

于 2021-06-13T00:01:48.183 回答