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

时间:2019-03-24 17:50:01

标签: javascript node.js converters

我正在通过几个联接查询一些数据,它返回给我一个单位 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"
                            }]

    }]

2 个答案:

答案 0 :(得分:0)

这样的事情会帮助您。但是需要提高房间级别的代码。直到建筑物和楼层都完成了

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);

答案 1 :(得分:0)

这是使用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>