无法使用javascript将嵌套json转换为平面json

时间:2019-07-16 05:16:29

标签: javascript jquery json

我在嵌套的json结构中具有如下所示的动态json

{
  "data": [
    {
      "_score": 0.5753642,
      "_type": "data",
      "_id": "686",
      "_source": {
        "mainData": "subset",
        "data": "vehicle",
        "name": "subset_vehicle",
        "fields": [
          "number",
          "id",
          "chasis"
        ],
        "dataset": "chasis"
      },
      "_index": "vehicle"
    },
   {
      "_score": 0.575,
      "_type": "data",
      "_id": "687",
      "_source": {
        "mainData": "subset1",
        "data": "vehicle1",
        "name": "subset_vehicle1",
        "fields": [
          "number",
          "id",
          "chasis"
        ],
        "dataset": "chasis1"
      },
      "_index": "vehicle"
    },
    {
      "_score": 0.57,
      "_type": "data",
      "_id": "686",
      "_source": {
        "mainData": "subset",
        "data": "vehicle",
        "name": "subset_vehicle",
        "fields": [
          "number",
          "id",
          "chasis"
        ],
        "dataset": "chasis"
      },
      "_index": "vehicle"
    }
  ]

}

我想将其转换为平面json类型

{
    "_id": "686",
     "mainData": "subset",
    "data": "vehicle",
    "dataset": "chasis"
    "name": "subset_vehicle",
    "fields": [
      "number",
      "id",
      "chasis"
    ]
}

因此,当我基于该ID单击前端侧的ID时,必须像上面那样转换特定对象,我的意思是平json。我习惯于以下逻辑来转换

var flatArray = [];
var flatObject = {};

for (var index = 0; index < data.length; index++) {
  for (var prop in data[index]) {

    var value = data[index][prop];

    if (Array.isArray(value)) {
      for (var i = 0; i < value.length; i++) {
        for (var inProp in value[i]) {
          flatObject[inProp] = value[i][inProp];
        }
      }
    }else{
        flatObject[prop] = value;
    }
  }
  flatArray.push(flatObject);
}

console.log(flatArray);

但我无法将其转换

2 个答案:

答案 0 :(得分:6)

您可以简单地使用Array.flatMap()Array.map()方法来返回一个新数组。根据您的要求,两者都可以使用。 Array.flatMap()Array.map()

var data = [{"_score":0.5753642,"_type":"data","_id":"686","_source":{"mainData":"subset","data":"vehicle","name":"subset_vehicle","fields":["number","id","chasis"],"dataset":"chasis"},"_index":"vehicle"},{"_score":0.575,"_type":"data","_id":"687","_source":{"mainData":"subset1","data":"vehicle1","name":"subset_vehicle1","fields":["number","id","chasis"],"dataset":"chasis1"},"_index":"vehicle"},{"_score":0.57,"_type":"data","_id":"686","_source":{"mainData":"subset","data":"vehicle","name":"subset_vehicle","fields":["number","id","chasis"],"dataset":"chasis"},"_index":"vehicle"}]

var a = data.flatMap(d => {
  d._source._id = d._id;
  return d._source;
})

console.log(a)

答案 1 :(得分:2)

您可以为此使用对象传播语法:

const data = [{"_score": 0.5753642,"_type": "data","_id": "686","_source": {"mainData": "subset","data": "vehicle","name": "subset_vehicle","fields": ["number","id","chasis"],"dataset": "chasis"},"_index": "vehicle"},{"_score": 0.575,"_type": "data","_id": "687","_source": {"mainData": "subset1","data": "vehicle1","name": "subset_vehicle1","fields": ["number","id","chasis"],"dataset": "chasis1"},"_index": "vehicle"},{"_score": 0.57,"_type": "data","_id": "686","_source": {"mainData": "subset","data": "vehicle","name": "subset_vehicle","fields": ["number","id","chasis"],"dataset": "chasis"},"_index": "vehicle"}];

const flatArray = data.map(({_id, _source}) => ({ _id, ..._source }));
console.log(flatArray);