使用下划线 js 将平面 JSON 转换为嵌套

Converting flat JSON to nested using underscore js

我正在使用几个连接查询一些数据,它 returns 我是一个单位 JSON 对象。我想做的是为 JSON。学习后探索发现了Underscore.js 还有一些,转换它看起来真的很复杂。

原始查询数据如下所示:

"data": [
    {
        "Building_Id": 1,
        "Building_Address": "i-9 ISLAMABAD",
        "Floor_Id": 1,
        "Floor_Name": "Ground",
        "Room_Id": 1,
        "Room_Name": "Bedroom"
    },
    {
        "Building_Id": 1,
        "Building_Address": "i-9 ISLAMABAD",
        "Floor_Id": 1,
        "Floor_Name": "Ground",
        "Room_Id": 2,
        "Room_Name": "TV Lounge"
    },
    {
        "Building_Id": 1,
        "Building_Address": "i-9 ISLAMABAD",
        "Floor_Id": 3,
        "Floor_Name": "1st Floor",
        "Room_Id": 3,
        "Room_Name": "Dining Room"
    },
    {
        "Building_Id": 2,
        "Building_Address": "G-10 ISLAMABAD",
        "Floor_Id": 4,
        "Floor_Name": "Ground",
        "Room_Id": 4,
        "Room_Name": "Bedroom"
    }
]

我想要的样子:

 "data": [
    {
        "Building_Id": 1,
        "Building_Address": "i-9 ISLAMABAD",
        "Floors": [{
                  "Floor_Id": 1,
                  "Floor_Name: "Ground",
                  "Rooms" : [{
                            "Room_Id": 1,
                            "Room_Name": "Bedroom"},
                            {
                            "Room_Id": 2,
                            "Room_Name": "TV Lounge"}
                            }]
                   },
                  "Floor_Id": 3,
                  "Floor_Name: "1st Floor",
                  "Rooms" : [{
                            "Room_Id": 3,
                            "Room_Name": "Dining Room"
                            }]

    }]

类似的内容会对您有所帮助。但需要改进房间级别的代码。这是在建筑物和楼层上完成的

const _ = require('lodash');

const flatData = {
  "data": [
    {
      "Building_Id": 1,
      "Building_Address": "i-9 islamabad",
      "Floor_Id": 1,
      "Floor_Name": "Ground",
      "Room_Id": 1,
      "Room_Name": "Bedroom"
    },
    {
      "Building_Id": 1,
      "Building_Address": "i-9 islamabad",
      "Floor_Id": 1,
      "Floor_Name": "Ground",
      "Room_Id": 2,
      "Room_Name": "TV Lounge"
    },
    {
      "Building_Id": 1,
      "Building_Address": "i-9 islamabad",
      "Floor_Id": 3,
      "Floor_Name": "1st Floor",
      "Room_Id": 3,
      "Room_Name": "Dining Room"
    },
    {
      "Building_Id": 2,
      "Building_Address": "G-10 Islamabad",
      "Floor_Id": 4,
      "Floor_Name": "Ground",
      "Room_Id": 4,
      "Room_Name": "Bedroom"
    }
  ]
}

const buildings = _.groupBy(flatData.data, "Building_Id");
const nestedData = [];
for (var key in buildings) {
  if (buildings.hasOwnProperty(key)) {
    nestedData.push({
      Building_Id: key,
      Building_Address: buildings[key][0].Building_Address,
      floors: _.map(buildings[key], floor => {
        return {
          Floor_Id: floor.Floor_Id,
          Floor_Name: floor.Floor_Name,
          Room_Id: floor.Room_Id,
          Room_Name: floor.Room_Name
        }
      })
    });
  }
}

console.log(nestedData);

这是使用 underscore.js 的一种方法:

var flatData = {
 "data": [
    {
        "Building_Id": 1,
        "Building_Address": "i-9 ISLAMABAD",
        "Floor_Id": 1,
        "Floor_Name": "Ground",
        "Room_Id": 1,
        "Room_Name": "Bedroom"
    },
    {
        "Building_Id": 1,
        "Building_Address": "i-9 ISLAMABAD",
        "Floor_Id": 1,
        "Floor_Name": "Ground",
        "Room_Id": 2,
        "Room_Name": "TV Lounge"
    },
    {
        "Building_Id": 1,
        "Building_Address": "i-9 ISLAMABAD",
        "Floor_Id": 3,
        "Floor_Name": "1st Floor",
        "Room_Id": 3,
        "Room_Name": "Dining Room"
    },
    {
        "Building_Id": 2,
        "Building_Address": "G-10 ISLAMABAD",
        "Floor_Id": 4,
        "Floor_Name": "Ground",
        "Room_Id": 4,
        "Room_Name": "Bedroom"
    }
  ]
};

var toNested = function(data) {
  return _.map(_.groupBy(data, 'Building_Id'), function(i) {
    return {
      Building_Id: i[0].Building_Id,
      Building_Address: i[0].Building_Address,
      Floors: _.map(_.groupBy(i, 'Floor_Id'), function(j) {   
        return {
          Floor_Id: j[0].Floor_Id,
          Floor_Name: j[0].Floor_Name,
          Rooms: _.map(j, function(k) {
            return {
              Room_Id: k.Room_Id,
              Room_Name: k.Room_Name
            };
          })
        };
      })
    };
  });
};

var nestedData = {
  "data": toNested(flatData.data)
};

console.log(nestedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>