2017-04-25 198 views
0
[ 
    { 
    "children": [ 
     { 
     "children": [ 
      { 
      "dateAdded": 1493033302670, 
      "id": "1534", 
      "index": 0, 
      "parentId": "1", 
      "title": "data1", 
      "url": "data2" 
      }, 
      { 
      "children": [ 
       { 
       "dateAdded": 1489571506844, 
       "id": "1451", 
       "index": 0, 
       "parentId": "1401", 
       "title": "data3", 
       "url": "data4" 
       } 
      ], 
      "dateAdded": 1490363326576, 
      "dateGroupModified": 1490363326576, 
      "id": "1401", 
      "index": 1, 
      "parentId": "1", 
      "title": "daily" 
      }, 
      { 
      "children": [ 
       { 
       "dateAdded": 1481787664555, 
       "id": "1429", 
       "index": 0, 
       "parentId": "1407", 
       "title": "data56", 
       "url": "data" 
       }, 
       { 
       "dateAdded": 1483365608504, 
       "id": "1430", 
       "index": 1, 
       "parentId": "1407", 
       "title": "data34", 
       "url": "data55" 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 
] 

這是Chrome書籤數據的表示形式。拼合嵌套的json對象

如果對象有url屬性它表示這是一個書籤。如果它沒有url屬性它是一個文件夾。

它是一個樹形結構。

我想使用附加屬性類型創建平坦對象。像:

[ 
{ 
    "dateAdded": 1489571506844, 
    "id": "1451", 
    "index": 0, 
    "parentId": "1401", 
    "title": "title", 
    "url": "some url", 
    "type": "bookmark" 

}, 
{ 
    "dateAdded": 1489571506844, 
    "id": "1451", 
    "index": 0, 
    "parentId": "1402", 
    "title": "title2", 
    "url": "some url2" 
    "type": "folder" 
} 
] 

在此先感謝。

回答

1

你可以使用獲得的平面數據迭代和遞歸方法。

function flatten(array) { 
 
    var result = []; 
 
    array.forEach(function iter(o) { 
 
     var temp = {}, 
 
      keys = Object.keys(o); 
 

 
     if (keys.length > 1) { 
 
      keys.forEach(function (k) { 
 
       if (k !== 'children') { 
 
        temp[k] = o[k]; 
 
       } 
 
      }); 
 
      temp.type = 'url' in o ? 'bookmark' : 'folder'; 
 
      result.push(temp); 
 
     } 
 
     Array.isArray(o.children) && o.children.forEach(iter); 
 
    }); 
 
    return result; 
 
} 
 

 
var data = [{ children: [{ children: [{ dateAdded: 1493033302670, id: "1534", index: 0, parentId: "1", title: "data1", url: "data2" }, { children: [{ dateAdded: 1489571506844, id: "1451", index: 0, parentId: "1401", title: "data3", url: "data4" }], dateAdded: 1490363326576, dateGroupModified: 1490363326576, id: "1401", index: 1, parentId: "1", title: "daily" }, { children: [{ dateAdded: 1481787664555, id: "1429", index: 0, parentId: "1407", title: "data56", url: "data" }, { dateAdded: 1483365608504, id: "1430", index: 1, parentId: "1407", title: "data34", url: "data55" }] }] }] }]; 
 

 
console.log(flatten(data));
.as-console-wrapper { max-height: 100% !important; top: 0; }

2

我已經做了一個遍歷包含對象的數組的函數。如果給定對象具有名爲children的屬性,則該函數自行調用。如果沒有,那麼它會被推到一個新的陣列flattenedBookmarks

解決方案

var flattenedBookmarks = []; 

flattenBookmarks(bookmarks); 

function flattenBookmarks(bookmarks) { 
    for (var i = 0; i < bookmarks.length; i++) { 
     var potentialBookmark = bookmarks[i]; 

     if (potentialBookmark.hasOwnProperty("url")) { 
      potentialBookmark.type = "bookmark"; 
     } else { 
      potentialBookmark.type = "folder"; 
     } 

     if (potentialBookmark.hasOwnProperty("children")) { 
      flattenBookmarks(potentialBookmark.children); 

      if (potentialBookmark.hasOwnProperty("dateGroupModified")) { 
       flattenedBookmarks.push(potentialBookmark); 
      } 
     } else { 
      flattenedBookmarks.push(potentialBookmark); 
     } 
    } 
} 

你或許應該從函數返回扁平陣列,而不是將其存儲在一個新的全球陣列flattenedBookmarks的,但至少這將讓你開始。

https://jsfiddle.net/s9ur35re/

1

的例子展示瞭如何做到這一點

data = [ 
 
    { 
 
    "children": [ 
 
     { 
 
     "children": [ 
 
      { 
 
      "dateAdded": 1493033302670, 
 
      "id": "1534", 
 
      "index": 0, 
 
      "parentId": "1", 
 
      "title": "data1", 
 
      "url": "data2" 
 
      }, 
 
      { 
 
      "children": [ 
 
       { 
 
       "dateAdded": 1489571506844, 
 
       "id": "1451", 
 
       "index": 0, 
 
       "parentId": "1401", 
 
       "title": "data3", 
 
       "url": "data4" 
 
       } 
 
      ], 
 
      "dateAdded": 1490363326576, 
 
      "dateGroupModified": 1490363326576, 
 
      "id": "1401", 
 
      "index": 1, 
 
      "parentId": "1", 
 
      "title": "daily" 
 
      }, 
 
      { 
 
      "children": [ 
 
       { 
 
       "dateAdded": 1481787664555, 
 
       "id": "1429", 
 
       "index": 0, 
 
       "parentId": "1407", 
 
       "title": "data56", 
 
       "url": "data" 
 
       }, 
 
       { 
 
       "dateAdded": 1483365608504, 
 
       "id": "1430", 
 
       "index": 1, 
 
       "parentId": "1407", 
 
       "title": "data34", 
 
       "url": "data55" 
 
       } 
 
      ] 
 
      } 
 
     ] 
 
     } 
 
    ] 
 
    } 
 
]; 
 
data2 = []; 
 
function search(data) { 
 
for (n in data) { 
 
    if (typeof data[n] == 'object') { 
 
    if (data[n].id != undefined) { 
 
    if (data[n].url != undefined) { 
 
    data[n].type="folder"; 
 
    } else { 
 
    data[n].type="bookmark"; 
 
    } 
 
    data2.push(data[n]); 
 
    } 
 
    search(data[n]); 
 
    } 
 
} 
 
} 
 
search(data); 
 
console.log(data2);

+0

能否請您解釋一下代碼,而不是僅僅把它放在這裏 – kaldoran