Присоединяйтесь и группируйте узлы Json

#javascript #node.js #json

Вопрос:

Мне нужно помочь сгруппировать и объединить данные из json с помощью Nodejs. На самом деле я использовал пакет от Nodejs с именем linq, и я хотел бы сгруппировать свои данные в соответствии с ключом «проблема», а также связать ключ «следующий» с ключом «текущий». Наконец, я хотел бы создать вложенный объект. Здесь данные json:

 [
  {
    next: 2,
    current: 1,
    problem: "Problem1",
    proposed_solution: "Solution1",
    response: "Yes",
  },
  {
    next: 3,
    current: 1,
    problem: "Problem1",
    proposed_solution: "Solution1",
    response: "No",
  },
  {
    next: 4,
    current: 2,
    problem: "Problem1",
    proposed_solution: "Solution2",
    response: "Yes",
  },
  {
    next: 5,
    current: 2,
    problem: "Problem1",
    proposed_solution: "Solution2",
    response: "No",
  },
  {
    next: 6,
    current: 3,
    problem: "Problem1",
    proposed_solution: "Solution3",
    response: "Yes",
  },
  {
    next: 7,
    current: 3,
    problem: "Problem1",
    proposed_solution: "Solution3",
    response: "No",
  },
  {
    current: 4,
    problem: "Problem1",
    proposed_solution: "Solution4",
    part: "2037103-016",
    type: "OrderPart",
  },
  {
    current: 5,
    problem: "Problem1",
    type: "CreateTicket",
  },
  {
    current: 6,
    problem: "Problem1",
    part: "Part1",
    type: "OrderPart",
  },
  {
    current: 7,
    problem: "Problem1",
    type: "CreateTicket",
  },
  {
    next: 2,
    current: 1,
    problem: "Problem2",
    proposed_solution: "Solution1",
    response: "Yes",
  },
  {
    next: 3,
    current: 1,
    problem: "Problem2",
    proposed_solution: "Solution1",
    response: "No",
  },
  {
    current: 3,
    problem: "Problem2",
    proposed_solution: "Solution2",
    part: "2037103-015",
    type: "OrderPart",
  },
  {
    next: 4,
    current: 2,
    problem: "Problem2",
    proposed_solution: "Solution3",
    response: "Yes",
  },
  {
    next: 5,
    current: 2,
    problem: "Problem2",
    proposed_solution: "Solution3",
    response: "No",
  },
  {
    current: 5,
    problem: "Problem2",
    type: "CreateTicket",
  },
  {
    next: 6,
    current: 4,
    problem: "Problem2",
    proposed_solution: "Solution4",
    response: "Yes",
  },
  {
    next: 7,
    current: 4,
    problem: "Problem2",
    proposed_solution: "Solution4",
    response: "No",
  },
  {
    current: 6,
    problem: "Problem2",
    type: "ProblemSolved",
  },
  {
    current: 7,
    problem: "Problem2",
    type: "CreateTicket",
  },
  {
    next: 2,
    current: 1,
    problem: "Problem3",
    proposed_solution: "Solution1",
    response: "Yes",
  },
  {
    next: 3,
    current: 1,
    problem: "Problem3",
    proposed_solution: "Solution1",
    response: "No",
  },
  {
    next: 4,
    current: 2,
    problem: "Problem3",
    proposed_solution: "Solution2",
    response: "Yes",
  },
  {
    next: 5,
    current: 2,
    problem: "Problem3",
    proposed_solution: "Solution2",
    response: "No",
  },
  {
    next: 6,
    current: 3,
    problem: "Problem3",
    proposed_solution: "",
    response: "Yes",
  },
  {
    next: 4,
    current: 3,
    problem: "Problem3",
    proposed_solution: "Solution3",
    response: "No",
  },
  {
    current: 4,
    problem: "Problem3",
    proposed_solution: "Solution4",
    part: "Part1;Part2",
    type: "OrderPart",
  },
  {
    current: 5,
    problem: "Problem3",
    proposed_solution: "Solution5",
    part: "Part1;Part2;Part3",
    type: "OrderPart",
  },
  {
    current: 6,
    problem: "Problem3",
    type: "ProblemSolved",
  },
]
 

Спасибо

Комментарии:

1. что вы подразумеваете под тем, что я хотел бы сгруппировать свои данные в соответствии с ключом «проблема», но также связать ключ «следующий» с ключом «текущий» . Не совсем понятно, чего именно вы хотите. Покажите, чего вы ожидаете.

2. @decpk: вероятно, хочет создать двусвязный список

Ответ №1:

Вот данные, которые нужно получить в файле excel

 [
    {
        "next": 2,
        "current": 1,
        "problem": "Problem 1",
        "proposed_solution": "Solution 1 ?",
        "response": "Yes"
    },
    {
        "next": 3,
        "current": 1,
        "problem": "Problem 1",
        "proposed_solution": "Solution 1 ?",
        "response": "No"
    },
    {
        "next": 4,
        "current": 2,
        "problem": "Problem 1",
        "proposed_solution": "Solution 1.1",
        "response": "Yes"
    },
    {
        "next": 5,
        "current": 2,
        "problem": "Problem 1",
        "proposed_solution": "Solution 1.1",
        "response": "No"
    },
    {
        "next": 6,
        "current": 3,
        "problem": "Problem 1",
        "proposed_solution": "Solution 1.2",
        "response": "Yes"
    },
    {
        "next": 7,
        "current": 3,
        "problem": "Problem1",
        "proposed_solution": "Solution 1.2",
        "response": "No"
    },
    {
        "current": 4,
        "problem": "Problem 1",
        "proposed_solution": "Solution order",
        "part": "ref",
        "type": "OrderPart"
    },
    {
        "current": 5,
        "problem": "Problem 1",
        "type": "CreateTicket"
    },
    {
        "next": 8,
        "current": 6,
        "problem": "Problem 1",
        "proposed_solution": "Solution 1.3",
        "response": "Yes"
    },
    {
        "next": 9,
        "current": 6,
        "problem": "Problem 1",
        "proposed_solution": "Solution 1.3",
        "response": "No"
    },
    {
        "current": 7,
        "problem": "Problem1",
        "type": "CreateTicket"
    },

    {
        "current": 8,
        "problem": "Problem 1",
        "proposed_solution": "Solution order",
        "part": "ref",
        "type": "OrderPart"
    },
    {
        "current": 9,
        "problem": "Problem 1",
        "type": "CreateTicket"
    },



   
    {
        "next": 2,
        "current": 1,
        "problem": "Problem 2",
        "proposed_solution": "Solution 1 ?",
        "response": "Yes"
    },
    {
        "next": 3,
        "current": 1,
        "problem": "Problem 2",
        "proposed_solution": "Solution 1 ?",
        "response": "No"
    },
    {
        "next": 4,
        "current": 2,
        "problem": "Problem 2",
        "proposed_solution": "Solution 1.1 ?",
        "response": "Yes"
    },
    {
        "next": 5,
        "current": 2,
        "problem": "Problem 2",
        "proposed_solution": "Solution 1.1 ?",
        "response": "No"
    },
    {
        "current": 4,
        "problem": "Problem 2",
        "proposed_solution": "Solution order",
        "part": "ref",
        "type": "OrderPart"
    },
    {
        "next": 6,
        "current": 5,
        "problem": "Problem 2",
        "proposed_solution": "Solution 1.2 ?",
        "response": "Yes"
    },
    {
        "next": 7,
        "current": 5,
        "problem": "Problem 2",
        "proposed_solution": "Solution 1.2 ?",
        "response": "No"
    },
    {
        "current": 6,
        "problem": "Problem 2",
        "type": "ProblemSolved"
    },
    {
        "current": 7,
        "problem": "Problem 2",
        "type": "CreateTicket"
    },


    {
        "next": 8,
        "current": 3,
        "problem": "Problem 2",
        "proposed_solution": "Solution 1.2 ?",
        "response": "Yes"
    },
    {
        "next": 9,
        "current": 3,
        "problem": "Problem 2",
        "proposed_solution": "Solution 1.2 ?",
        "response": "No"
    },
  
   
    {
        "next": 10,
        "current": 8,
        "problem": "Problem 2",
        "proposed_solution": "Solution 1.3 ?",
        "response": "Yes"
    },
    {
        "next":11,
        "current":8,
        "problem": "Problem 2",
        "proposed_solution": "Solution 1.3 ?",
        "response": "No"
    },
    {
        "current": 10,
        "problem": "Problem 2",
        "proposed_solution": "Solution order",
        "part": "ref",
        "type": "OrderPart"
    },
    {
        "current": 11,
        "problem": "Problem 2",
        "type": "ProblemSolved"
    },
    {
        "current": 9,
        "problem": "Problem 2",
        "proposed_solution": "Solution order",
        "part": "ref",
        "type": "OrderPart"
    }    
]
 

Вот пример результата, который я хотел бы

 [
{
    "doc_id":"doc1",
    "body":"Problem 1",
    "answer":{
        "id":2,
        "no":{
            "id":3,
            "question":"Solution 1.2",
            "yes":{
                "id":4,
                "question":"Solution 1.3",
                "yes":{
                    "action": {
                        "message": "Solution order",
                        "part": ["ref"],
                        "type": "OrderPart"
                    }
                },
                "no":{
                    "action": {
                        "type": "CreateTicket"
                      }
                }
            },
            "no":{
                "action": {
                    "type": "CreateTicket"
                  }
            }
        }
    }
},
{
    "doc_id":"doc2",
    "body":"Problem 2",
    "answer":{
        "id":2,
        "question":"Solution 1 ?",
        "yes":{
           "question":"Solution 1.1",
           "id":3,
           "yes":{
            "action": {
                "message": "Solution order",
                "part": ["ref"],
                "type": "OrderPart"
              }
           },
           "no":{
                "question":"Solution 1.2 ",
                "id":4,
                "yes":{
                    "action": {
                        "type": "ProblemSolved"
                      }
                },
                "no":{
                    "action": {
                        "type": "CreateTicket"
                    }
                }
           }
        },
        "no":{
            "id":3,
            "question":"Solution 1.2",
            "yes":{
                "id":4,
                "question":"Solution 1.3",
                "yes":{
                    "action": {
                        "message": "Solution order",
                        "part": ["ref"],
                        "type": "OrderPart"
                    }
                },
                "no":{
                    "action": {
                        "type": "ProblemSolved"
                      }
                }
            },
            "no":{
                "action": {
                    "message": "Solution order",
                    "part": ["ref"],
                    "type": "OrderPart"
                }
            }
        }
    }
}
]