【问题标题】:Restructuring existing JSON based on category in react nativeReact Native中基于类别重构现有JSON
【发布时间】:2019-01-15 11:06:32
【问题描述】:

我有一个从服务器获得的 JSON 响应,如下所示

    {
  "questionList": [{
      "qno": "1",
      "state_name": "State1",
      "category_name": "Category1",
      "question_type": "type1",
      "question": "This is question 11",
      "options": ["No", "yes "]
    },
    {
      "qno": "2",
      "state_name": "State1",
      "category_name": "Category1",
      "question_type": "type1",
      "question": "This is question12",
      "options": ["No", "yes "]
    },
    {
      "qno": "3",
      "state_name": "State1",
      "category_name": "Category2",
      "question_type": "type2",
      "question": "This is question 21",
      "options": ["No ", "yes "]
    },
    {
      "qno": "4",
      "state_name": "State1",
      "category_name": "Category3",
      "question_type": "type1",
      "question": "This is question 31",
      "options": ["No ", "yes "]
    },
    {
      "qno": "5",
      "state_name": "State1",
      "category_name": "Category3",
      "question_type": "type1",
      "question": "This is question 32",
      "options": ["No ", "yes "]
    }
  ]
}

现在我想根据类别对其进行重组,以便具有多个相同类别的问题将属于一个类别,并且我自己的变量也很少。下面是它应该是什么样子的示例输出。

    [
  {
    "state": "State1",
    "category": "Category1",
    "questions": [
      {
        "questionID": "1",
        "question": "This is question 11",
        "options": ["No ", "yes "],
        "status": 0,
        "files": [],
        "questionType": "type1"
      },
     {
        "questionID": "2",
        "question": "This is question12",
        "options": ["No ", "yes "],
        "status": 0,
        "files": [],
        "questionType": "type1"
      }
    ]
  },
  {
    "state": "State1",
    "category": "Category2",
    "questions": [
      {
        "questionID": "3",
        "question": "This is question 21",
        "options": ["No ", "yes "],
        "status": 0,
        "files": [],
        "questionType": "type2"
      }

    ]
  },
{
    "state": "State1",
    "category": "Category3",
    "questions": [
      {
        "questionID": "4",
        "question": "This is question 31",
        "options": ["No ", "yes "],
        "status": 0,
        "files": [],
        "questionType": "type1"
      },
     {
        "questionID": "5",
        "question": "This is question 32",
        "options": ["No ", "yes "],
        "status": 0,
        "files": [],
        "questionType": "type1"
      }
    ]
  }
]

我并不是 JSON 操作方面的专家。谁能告诉我这应该是解决这个问题的最佳方法吗?

【问题讨论】:

  • 一个类别可以有多个状态吗?
  • 不,特定类别只允许一个状态。

标签: android ios json reactjs react-native


【解决方案1】:

因此,如果我们从您提供的 json 中获取 questionList 数组,我们可以执行以下操作。

  1. 遍历 questionList 数组中的每个问题。
  2. 将问题转换为新格式。
  3. 检查我们以前是否遇到过该类别。
    • 如果没有,我们将创建类别,将新问题添加到其中,并将类别添加到结果数组中。
    • 如果我们之前找到了该类别,我们将在结果数组中找到它并将新问题添加到其中。

这里是代码

let data = [{qno: "1", state_name: "State1", ... }, ... ] // notice that this is the questionList array and not an object
let categories = [];     // to track the categories that you have found
let result = [];        // where your results will be stored

data.forEach(question => {

  // create the question
  let newQuestion = {};
  newQuestion.questionID = question.qno;
  newQuestion.question = question.question;
  newQuestion.options = question.options;
  newQuestion.status = 0;
  newQuestion.files = [];
  newQuestion.questionType = question.question_type;

  // check to see if we have the category, if not create it
  if (categories.indexOf(question.category_name) === -1) {
    // create a new category
    let newCategory = {};
    newCategory.state = question.state_name;
    newCategory.category = question.category_name;
    newCategory.questions = [];

    newCategory.questions.push(newQuestion);  // add the question to the category we just created
    result.push(newCategory);                 // add the category to the result
    categories.push(question.category_name);  // track the category so we know not to create a new one
  } else {
    // search for the category
    let foundCategory = result.filter(group => group.category === question.category_name);
    // if we have found a category add the new question to it
    if (foundCategory.length) {
      foundCategory[0].questions.push(newQuestion);
    }
  }
});

console.log(result);

【讨论】:

  • 谢谢,效果很好:)。只是为了让您知道代码中的变量名结果存在拼写错误,您可以根据需要进行编辑;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
相关资源
最近更新 更多