如何加入/合并json结果

时间:2018-07-17 10:20:21

标签: javascript json ecmascript-6

为减少发送给客户端的有效负载,我的rest api仅返回如下所示的子实体的ID:

[  
   {  
      "id":369,
      "name":"Harlequin Enterprises Ltd",
      "description":"Buford Konopelski",
      "parent":{  
         "id":323
      }
   },
   {  
      "id":323,
      "name":"Heyday Books",
      "description":"Zola Rutherford",
      "parent":{  
         "id":3
      }
   },
   {  
      "id":3,
      "name":"Happy House",
      "description":"Mrs. Dwight Mohr",
      "parent":{  
         "id":null
      }
   }
]

有没有图书馆可以帮助我加入/合并所有实体:

[  
   {  
      "id":369,
      "name":"Harlequin Enterprises Ltd",
      "description":"Buford Konopelski",
      "parent":{  
         "id":323,
         "name":"Heyday Books",
         "description":"Zola Rutherford",
         "parent":{  
            "id":3,
            "name":"Happy House",
            "description":"Mrs. Dwight Mohr",
            "parent":{  
               "id":null
            }
         }
      }
   },
   {  
      "id":323,
      "name":"Heyday Books",
      "description":"Zola Rutherford",
      "parent":{  
         "id":3,
         "name":"Happy House",
         "description":"Mrs. Dwight Mohr",
         "parent":{  
            "id":null
         }
      }
   },
   {  
      "id":3,
      "name":"Happy House",
      "description":"Mrs. Dwight Mohr",
      "parent":{  
         "id":null
      }
   }
]

//以下是受IVO GELOV解决方案启发的简化解决方案:

var mapID = inputJSON.map((item) => item.id);
// attach parents to childreen
var normalized = inputJSON.map((item) =>
{
    var parentIndex = mapID.indexOf(item.parent.id);
    item.parent = inputJSON[parentIndex];
    return item;
});
console.log(JSON.stringify(normalized))

4 个答案:

答案 0 :(得分:1)

您必须使用地图

var inputJSON, mapID = {};

// create the map - allowing to get an item by ID
inputJSON.forEach((item) =>
{
  mapID[item.id] = item;
});

// attach parents to childreen
inputJSON.forEach((item) =>
{
  if(item.parent && item.parent.id && mapID[item.parent.id]) 
  {
    item.parent = mapID[item.parent.id]
  }
});

答案 1 :(得分:0)

您可以递归执行此操作,但是您必须介意性能(或不可以)。

var arr = [{
    "id": 369,
    "name": "Harlequin Enterprises Ltd",
    "description": "Buford Konopelski",
    "parent": {
      "id": 323
    }
  },
  {
    "id": 323,
    "name": "Heyday Books",
    "description": "Zola Rutherford",
    "parent": {
      "id": 3
    }
  },
  {
    "id": 3,
    "name": "Happy House",
    "description": "Mrs. Dwight Mohr",
    "parent": {
      "id": null
    }
  }
];

function findId(a, id) {
  var elem = null;
  a.forEach((element) => {
    if (element.id === id) {
      elem = JSON.parse(JSON.stringify(element));
      if (null !== element.parent.id) {
        var parentElem = findId(a, element.parent.id);
        elem.parent = JSON.parse(JSON.stringify(parentElem));
      }
    }
  });
  return elem;
}
var newArr = [];
arr.forEach((element) => {
  var elem = findId(arr, element.id);
  newArr.push(elem);
});

console.log(newArr);

答案 2 :(得分:0)

您可以使用for循环

var final_Array = [];
var data = [  
   {  
      "id":369,
      "name":"Harlequin Enterprises Ltd",
      "description":"Buford Konopelski",
      "parent":{  
         "id":323
      }
   },
   {  
      "id":323,
      "name":"Heyday Books",
      "description":"Zola Rutherford",
      "parent":{  
         "id":3
      }
   },
   {  
      "id":3,
      "name":"Happy House",
      "description":"Mrs. Dwight Mohr",
      "parent":{  
         "id":null
      }
   }
]

for(var i=0;i<data.length;i++){
    var f = 0;
    for(var j=0;j<data.length;j++){
        if(data[i].parent.id==data[j].id){
            var r = {
                 "id":data[i].id,
                 "name":data[i].name,
                 "description":data[i].description,
                 "parent":data[j]
            }
            final_Array.push(r);
        }
        else if(data[i].parent.id==null){
            f++;        
        }
    }
    if(j==f){
            var r = {
                 "id":data[i].id,
                 "name":data[i].name,
                 "description":data[i].description,
                 "parent":data[i].parent
            }
            final_Array.push(r);
    }
}
if(i==data.length){
    console.log(final_Array);
}

希望这会有所帮助

答案 3 :(得分:0)

您可以使用Array.forEach进行迭代,并使用Array.reduce基于parent键构造每个对象。

var arr = [  
   {  
      "id":369,
      "name":"Harlequin Enterprises Ltd",
      "description":"Buford Konopelski",
      "parent":{  
         "id":323
      }
   },
   {  
      "id":323,
      "name":"Heyday Books",
      "description":"Zola Rutherford",
      "parent":{  
         "id":3
      }
   },
   {  
      "id":3,
      "name":"Happy House",
      "description":"Mrs. Dwight Mohr",
      "parent":{  
         "id":null
      }
   }
];

arr.forEach(obj => {
    obj = arr.reduce(acc => {
        var parent=acc.parent;
        while(parent.parent) parent=parent.parent;
        if(parent.id)
        {
            Object.assign(parent, arr.find(function(o){return o.id==parent.id}));
        }
        return acc;
    })
});

console.log(arr)