Javascript lodash压平分层对象

时间:2016-03-07 09:57:46

标签: javascript angularjs json node.js lodash

我有层次json。我想压扁它。 这是我的json:

{
  "id": "111",
  "name": "v5",
  "define": {
    "system": "abc",
    "concept": [{
      "code": "y7",
      "concept": [{
        "code": "AGG",
        "display": "Abcess"
      }, {
        "code": "ABS",
        "display": "Abcess"
      }]
    }, {
      "code": "y8",
      "concept": [{
        "code": "AGc",
        "display": "ccc"
      }, {
        "code": "hjj",
        "display": "uii"
      }]
    }]
  }
}

lodash中是否有办法将此json展平为“代码,显示”数组?

如果不是,是否有其他库可以执行此操作? 感谢。

例如结果:

[{
  "code": "AGG",
  "display": "Abcess"
}, {
  "code": "ABS",
  "display": "Abcess"
}, {
  "code": "AGc",
  "display": "ccc"
}, {
  "code": "hjj",
  "display": "uii"
}]

2 个答案:

答案 0 :(得分:1)

  

在lodash中是否有一种方法可以将这个json扁平化为"代码数组,   desplay"仅?

你的意思是连接所有concept个孩子?

试试这个



var obj = {
  "id": "111",
  "name": "v5",
  "define": {
    "system": "abc",
    "concept": [{
      "code": "y7",
      "concept": [{
        "code": "AGG",
        "display": "Abcess"
      }, {
        "code": "ABS",
        "display": "Abcess"
      }]
    }, {
      "code": "y8",
      "concept": [{
        "code": "AGc",
        "display": "ccc"
      }, {
        "code": "hjj",
        "display": "uii"
      }]
    }]
  }
}

var output = [];

obj.define.concept.forEach(function(o) {
  output = output.concat(o.concept)
});

document.write("<pre>" + JSON.stringify(output, 0, 4) + "</pre>");
&#13;
&#13;
&#13;

答案 1 :(得分:1)

Array#forEach()的解决方案。

    <html>
         <head>
         <script type="text/javascript" src="https://www.google.com/jsapi"></script>
         <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
         google.load('visualization', '1', {'packages': ['table', 'corechart','controls']});
         google.setOnLoadCallback(initialize);

    function initialize() {
              // The URL of the spreadsheet to source data from.
              var query = new google.visualization.Query(
                'https://docs.google.com/spreadsheets/d/1AgwPjUk1VOv48dV_5MjTY8286vf8lomJRZ8K0cj2Mgg/edit#gid=0');
              query.setQuery("select A, G");
              query.send(draw);
             }
             function draw(response) {
                if (response.isError()) {
                    alert('Error in query');
                }
                var data = response.getDataTable();
                var format1 = new google.visualization.DateFormat({pattern: "HHmmss"});
                format1.format(data, 0);
                var view = new google.visualization.DataView(data);
                var table = new google.visualization.Table(document.getElementById('table_div'));
                view.setColumns([0,1]);
                // table.draw(view);
                // to get the values from the view to array arr 
                var i;
                var arr = [];
                var val = [];
                for(i = 0; i< view.getNumberOfRows(); i++){
                    arr.push(view.getValue(i,0));
                    val.push(view.getValue(i,1));
                }
                var x,cost =0, unit =0;
                for(x in arr){
                    if(arr[x] >= 60000 && arr[x] <= 180000){
                        unit = val[x] * (1/60);
                        cost = cost + (unit * 595/100);
                        unit = 0;
                    }
                    else if(arr[x] >= 180000 && arr[x] <= 220000){
                        unit = val[x] * (1/60);
                        cost = cost + (unit *720/100);
                        unit = 0;
                    }
                    else{
                        unit = val[x] * (1/60);
                        cost = cost + (unit * 820/100);
                        unit = 0;
                    }
                }
                document.write(cost);
             }
            </script>
           </head>
           <body>
            <div id="table_div">
            </div>
           </body>
           </html>

相关问题