如何解析嵌套数据json数据以探索angularjs中xslx格式的文件

时间:2017-02-22 09:57:29

标签: angularjs

  

我是angularjs的新手我需要探索xlsx格式的数据但是   我的数据是嵌套的json数组格式,这是不可能的   以xlsx格式探索数据。

index.html

slidesOffsetBefore

export.js

<script src="https://rawgithub.com/eligrey/FileSaver.js/master/FileSaver.js" type="text/javascript"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.12/xlsx.core.min.js"></script>
      <script src="https://cdn.jsdelivr.net/alasql/0.3/alasql.min.js"></script>
    <div class="box-body">
    <button ng-click="exportData()" class="btn btn-primary" style="float:right;margin-right:30px; margin-bottom:30px;">Export&nbsp;&nbsp;<i class="fa fa-arrow-do
wn"></i></button>
</div>

请让我知道如何使用angularjs以xlsx格式导出此数据我已经尝试了从24小时以来的所有方式但我没有得到怎么办请使用我的json数据,我已经给了我工作小提琴它我会在angularjs中为我提供帮助,只需要以xlsx格式导出我的数据。

1 个答案:

答案 0 :(得分:0)

您可以将复杂的json展平为简单如下。

var data = [
     {
         "InvDetails": "UPS",
         "LstRecords": [
           {
               "Id": 1,
               "Invertor_Id": 1,
               "Time_of_Reading": "20170214",
               "Lastreading": 0,
               "Readingby": 0
           },
           {
               "Id": 87,
               "Invertor_Id": 1,
               "Time_of_Reading": "20170215",
               "Lastreading": 5,
               "Readingby": 10
           },
           {
               "Id": 110,
               "Invertor_Id": 1,
               "Time_of_Reading": "20170216",
               "Lastreading": 10,
               "Readingby": 92
           },
           {
               "Id": 111,
               "Invertor_Id": 1,
               "Time_of_Reading": "20170216",
               "Lastreading": 92,
               "Readingby": 95
           }
         ]
     },
     {
         "InvDetails": "Power Supply",

         "LstRecords": [
           {
               "Id": 2,
               "Invertor_Id": 2,
               "Time_of_Reading": "20170214",
               "Lastreading": 0,
               "Readingby": 0
           },
           {
               "Id": 88,
               "Invertor_Id": 2,
               "Time_of_Reading": "20170215",
               "Lastreading": 7,
               "Readingby": 13
           },
           {
               "Id": 109,
               "Invertor_Id": 2,
               "Time_of_Reading": "20170216",
               "Lastreading": 13,
               "Readingby": 25
           },
           {
               "Id": 112,
               "Invertor_Id": 2,
               "Time_of_Reading": "20170216",
               "Lastreading": 25,
               "Readingby": 49
           }
         ]
     }
    ];

然后为

创建Array方法的原型
Array.prototype.concatAll = function() {
  var results = [];
  this.forEach(function(subArray){
      subArray.forEach(function(item) {
          results.push(item);
      });
  })
  return results;
};

之后,你可以简单地使用数组映射函数将复杂的json展平为简单的

var newData = 
      data.
        map(function(invertor){
          return invertor.LstRecords;
        }).
        concatAll();
console.log(newData)