1、目前有一個很任性的介面API,它提供的資料非常不合理
2、資料如下
var json = [{"mainId": 1, "title": "abc", "createdate": "2017-06-28"},{"mainId": 2, "title": "ddddd", "createdate": "2017-06-25"}];
var json1 = [{"childId":1, "mainId": 1, "childname": "cname"},{"childId":2, "mainId": 1, "childname": "cname2"},{"childId":3, "mainId": 2, "childname": "cname3"},{"childId":4, "mainId": 2, "childname": "cname4"}];
var json2 = [{"childchildId":1, "childId": 1, "childname": "cname"},{"childchildId":2, "childId": 1, "childname": "cname2"},{"childchildId":3, "childId": 2, "childname": "cname3"},{"childchildId":4, "childId": 2, "childname": "cname4"},{"childchildId":5, "childId": 3, "childname": "cname"},{"childchildId":6, "childId": 3, "childname": "cname2"},{"childchildId":7, "childId": 4, "childname": "cname3"},{"childchildId":8, "childId": 4, "childname": "cname4"}];
3、我想將它們合併成一個json如下:
var result = [{"mainId":1, "title": "abc", "createdate": "2017-06-28", child:[{"childId":1, "mainId": 1, "childname": "cname", childchild:[{"childchildId":1, "childId": 1, "childname": "cname"},{"childchildId":2, "childId": 1, "childname": "cname2"}]},{"childId":2, "mainId": 1, "childname": "cname2", childchild:[{"childchildId":3, "childId": 2, "childname": "cname3"},{"childchildId":4, "childId": 2, "childname": "cname4"}]}]
是否有好的辦法?
額,就是不想寫循環
reduce重構下:
php的,一步步來
正常輸出
很好用的,不過瀏覽器可能會提示eval是有害的,這個想要去掉,可以百度有方法,具體我忘了