使用json2csv

时间:2018-04-02 13:53:05

标签: javascript json node.js object recursion

使用nodejs,我正在尝试以这样的方式格式化我的json,它可以用来使用json2csv作为csv下载。

json中的问题示例是答案[“4”] [“text”]。 json中的答案示例是答案[“4”] [“答案”]。但并非所有问题都有答案。

我最终想要得到的是:

arrayQuestions = ["Welkom", "Het spijt ons dit te horen", "Wat is je email adres", "Hoe was je ervaring", "Click on next to continue", "Dankjewel"]; 
arrayAnswers = [ { 
                  answer1: "", 
                  answer2: "", 
                  answer3: "email@email.com", 
                  answer4: "4", 
                  answer5: "", 
                  answer6: "" }, 
                { answer1: "", 
                  answer2: "", 
                  answer3: "email2@email.com", 
                  answer4: "5", 
                  answer5: "", 
                  answer6: "" }];

到目前为止,我设法循环遍历json及其许多嵌套对象,并返回带有问题和答案的所有对象。但是,它没有考虑到不同的条目,我正在努力做到正确。

希望任何人都可以提供帮助:)

    var json = {
      "content": [{
          "updated_at": "2018-03-01 06:38:16",
          "answers": {
            "2": {
              "name": "clickTo",
              "text": "Welkom",
              "type": "control_text"
            },
            "3": {
              "name": "hetSpijt3",
              "text": "Het spijt ons dit te horen. ",
              "type": "control_textarea"
            },
            "4": {
              "name": "watIs",
              "text": "Wat is je email adres?",
              "type": "control_email",
              "answer": "email@email.com"
            },
            "7": {
              "name": "hoeWas",
              "text": "Hoe was je ervaring",
              "type": "control_rating",
              "answer": "4"
            },
            "8": {
              "name": "clickTo8",
              "text": "Click on next to continue",
              "type": "control_text"
            },
            "10": {
              "name": "dankjewel10",
              "text": "Dankjewel. ",
              "type": "control_textarea"
            }
          }
        },
        {
          "updated_at": "2018-03-07 14:15:45",
          "answers": {
            "2": {
              "name": "clickTo",
              "text": "Welkom",
              "type": "control_text"
            },
            "3": {
              "name": "hetSpijt3",
              "text": "Het spijt ons dit te horen. ",
              "type": "control_textarea"
            },
            "4": {
              "name": "watIs",
              "text": "Wat is je email adres?",
              "type": "control_email",
              "answer": "email2@email.com"
            },
            "7": {
              "name": "hoeWas",
              "text": "Hoe was je ervaring?",
              "type": "control_rating",
              "answer": "5"
            },
            "8": {
              "name": "clickTo8",
              "text": "Click on next to continue",
              "type": "control_text"
            },
            "10": {
              "name": "dankjewel10",
              "text": "Dankjewel. ",
              "type": "control_textarea"
            }
          }
        }
      ]
    };


    var answersArray = [];

    iterObj(json);

    console.log(answersArray);

    function iterObj(obj) {

      for (var key in obj) {
        if (obj[key].hasOwnProperty("text")) {
          answersArray.push(obj[key]);
        }
        if (obj[key] !== null && typeof obj[key] === "object") {
          iterObj(obj[key]);
        }
      }
    }

0 个答案:

没有答案