【问题标题】:Parsed Json array shows undefined in Dialogflow已解析的 Json 数组在 Dialogflow 中显示未定义
【发布时间】:2021-04-15 05:15:02
【问题描述】:

以下是我的 json 数据:

{
    "code": "200",
    "count": 3,
    "data": [
        {
            "_id": "602a2d4427e5ac1f5c8e897b",
            "survey_id": "602a5de91d4f0000ec004b94",
            "parent_question_id": "",
            "is_required": "1",
            "is_active": "1",
            "options": [
                {
                    "_id": "604b22c53b57d53fdc8c43f3",
                    "question_id": "602a2d4427e5ac1f5c8e897b",
                    "option_type_id": "602a2b0127e5ac1f5c8e8976",
                    "option_details": "He/She is good.",
                    "is_active": "1"
                },
                {
                    "_id": "604b22df3b57d53fdc8c43f5",
                    "question_id": "602a2d4427e5ac1f5c8e897b",
                    "option_type_id": "602a2b0127e5ac1f5c8e8976",
                    "option_details": "He/She is Average.",
                    "is_active": "1"
                },
                {
                    "_id": "604b22e63b57d53fdc8c43f6",
                    "question_id": "602a2d4427e5ac1f5c8e897b",
                    "option_type_id": "602a2b0127e5ac1f5c8e8976",
                    "option_details": "He/She is Poor.",
                    "is_active": "1"
                },
                {
                    "_id": "604b22d33b57d53fdc8c43f4",
                    "question_id": "602a2d4427e5ac1f5c8e897b",
                    "option_type_id": "602a2b0127e5ac1f5c8e8976",
                    "option_details": "He/She is Excellent.",
                    "is_active": "1"
                }
            ],
            "qustion_type": {
                "_id": "602a2b2627e5ac1f5c8e8978",
                "question_type": "Multie Radio",
                "is_active": "1"
            }
        },
    ]
}

这里我想获取“option”数组中的“option_details”的数据。我可以得到选项数组。

以下是我的代码:

return 
  axios.get(api call here)
  .then((results) => {
    results.data.data.map(quesobj => {
      ques.push(quesobj.question_text);
    })
    console.log(ques);
    results.data.data.map(questypeobj => {
      questype.push(questypeobj.qustion_type.question_type);
    })
    results.data.data.map(optionobj => {
      option.push(optionobj.options);
    })
    console.log("Hello");
    console.log(option);
    console.log(option[0]);
    console.log(option[0].option_details);
    conv.add('Thank you');
  })

在控制台中,当我打印选项时,它会显示来自 api 的选项数组。但是当我打印 option[0] 时,它会出现 [] 而当我打印 option[0].option_details 时,它会出现未定义。我该如何解决?

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: node.js actions-on-google dialogflow-es-fulfillment


    【解决方案1】:

    发生了什么

    在您的 json 数据中,options 是一个数组。所以,这一行:

    option.push(optionobj.options)

    会将一些数组推送到您的option 变量。最后option是一个array of arrayoption[0]是一个数组,option[0].option_details是未定义的。

    建议

    如果我理解正确,您希望 option 成为一个数组,其中包含来自 options 属性的所有项目。您可以使用扩展运算符来做到这一点,从 :

    option.push(optionobj.options)

    到:

    option.push(...optionobj.options);

    扩展运算符会将optionobj.options 中的所有项目(不是数组本身,这就是区别)推送到option

    您可以找到有关扩展语法here的更多信息

    【讨论】:

    • 对不起,这没有帮助
    • 当您尝试的结果与您的预期不符时,它做了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 2015-12-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多