Javascript
 Computer >> コンピューター >  >> プログラミング >> Javascript

JavaScriptでツリーをDB形式からJSON形式に変換する


このようなオブジェクトの配列があるとします-

const arr = [
   {"id":7,"name":"Kuwait","parentId":2},
   {"id":4,"name":"Iraq","parentId":2},
    {"id":10,"name":"Qatar","parentId":2},
   {"id":2,"name":"Middle East","parentId":1},
   {"id":3,"name":"Bahrain","parentId":2},
   {"id":6,"name":"Jordan","parentId":2},
   {"id":8,"name":"Lebanon","parentId":2},
   {"id":1,"name":"Africa/Middle East","parentId":null},       {"id":5,"name":"Israel","parentId":2},
    {"id":9,"name":"Oman","parentId":2}
];

そのような配列を1つ取り込むJavaScript関数を作成する必要があります。次に、関数は、親に従ってグループ化されたオブジェクトを含む新しい配列を準備する必要があります。

したがって、出力は次のようになります-

const output = [
   {"id":55,"text":"Africa/Middle East","children":[
   {"id":2,"text":"Middle East","children":
   [{"id":7,"name":"Kuwait","children":[]},
   {"id":4,"name":"Iraq","children":[]},
   {"id":10,"name":"Qatar","children":[]},
   {"id":3,"name":"Bahrain","children":[]},
   {"id":6,"name":"Jordan","children":[]},
   {"id":8,"name":"Lebanon","children":[]},
   {"id":5,"name":"Israel","children":[]},
   {"id":9,"name":"Oman","children":[]}]}
];

const arr = [
   {"id":7,"name":"Kuwait","parentId":2},
    {"id":4,"name":"Iraq","parentId":2},
    {"id":10,"name":"Qatar","parentId":2},
   {"id":2,"name":"Middle East","parentId":1},
    {"id":3,"name":"Bahrain","parentId":2},
    {"id":6,"name":"Jordan","parentId":2},
   {"id":8,"name":"Lebanon","parentId":2},
    {"id":1,"name":"Africa/Middle East","parentId":null},    {"id":5,"name":"Israel","parentId":2},
    {"id":9,"name":"Oman","parentId":2} ];
const transformTree = (data, root = null) => {
   const res = [];
   const map = {};
   data.forEach((el) => {
      el.children = map[el.id] && map[el.id].children || [];
      map[el.id] = el; if (el.parentId === root) {
          res.push(el);
      }
      else {
         map[el.parentId] = map[el.parentId] || {};
         map[el.parentId].children = map[el.parentId].children || [];                map[el.parentId].children.push(el);
      };
   });
   return res;
};
console.log(JSON.stringify(transformTree(arr), undefined, 4));

出力

そして、コンソールの出力は-

になります
[
   { "id": 1,
      "name": "Africa/Middle East",
      "parentId": null,
      "children": [
      {
         "id": 2,
         "name": "Middle East",
         "parentId": 1,
         "children": [
            {
               "id": 7,
               "name": "Kuwait",
               "parentId": 2,
               "children": []
            },
            {
               "id": 4,
               "name": "Iraq",
               "parentId": 2,
            "children": []
         },
         {
            "id": 10,
            "name": "Qatar",
            "parentId": 2,
            "children": []
      }, {
            "id": 3,
            "name": "Bahrain",
            "parentId": 2,
            "children": []
            },
            {
               "id": 6,
               "name": "Jordan",
               "parentId": 2,
               "children": []
            }, {
                  "id": 8,
                  "name": "Lebanon",
                  "parentId": 2,
                  "children": []
            }, {
                  "id": 5,
                  "name": "Israel",
                  "parentId": 2,
                  "children": []
               }, {
                     "id": 9,
                     "name": "Oman",
                     "parentId": 2,
                     "children": []
               }
            ]
         }
      ]
   }
]

  1. JavaScriptJSON配列

    JSONの配列は、JavaScriptの配列に似ています。 JavaScriptJSON配列は次のようになります- let obj = {    name:'Rohan',    sports : ['cricket','Football','volleyball','hockey'] } 以下はJavaScriptのJSON配列のコードです- 例 <!DOCTYPE html> <html lang="en"> <head

  2. JavaScriptを使用してJSON配列からデータを読み取る方法は?

    以下は、JavaScriptを使用してJSON配列からデータを読み取るためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style