const curCollection = {
"info": {
"_postman_id": "d4147e0c-943c-4899-8d5b-e738e7109796",
"name": "testBody",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "folder11",
"item": [
{
"name": "folder2",
"item": [
{
"name": "folder3",
"item": [
{
"name": "3层",
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:3000/api/requests",
"protocol": "http",
"host": [
"localhost"
],
"port": "3000",
"path": [
"api",
"requests"
]
}
},
"response": []
}
],
"_postman_isSubFolder": true
},
{
"name": "11",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "baidu.com",
"host": [
"baidu",
"com"
]
}
},
"response": []
}
],
"_postman_isSubFolder": true
},
{
"name": "r1",
"request": {
"method": "DELETE",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "baidu.com",
"host": [
"baidu",
"com"
]
},
"description": "11"
},
"response": []
}
]
},
{
"name": "folder2",
"item": [
{
"name": "f2-1",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "tieba.baidu.com",
"host": [
"tieba",
"baidu",
"com"
]
}
},
"response": []
},
{
"name": "f2-2",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "tieba.baidu.com",
"host": [
"tieba",
"baidu",
"com"
]
}
},
"response": []
}
]
},
{
"name": "baidu.com",
"event": [
{
"listen": "prerequest",
"script": {
"id": "d1ad65b0-528d-4b3d-a119-24eb842f57c8",
"exec": [
"var a = 12;",
"var b= 13;"
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
"id": "029cfa5c-4083-4f21-8e8a-6597382f5f9c",
"exec": [
"let i = 100;"
],
"type": "text/javascript"
}
}
],
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [
{
"key": "Accept-Charset",
"value": "utf-8",
"type": "text",
"disabled": true
},
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/x-www-form-urlencoded",
"type": "text",
"disabled": true
}
],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "a",
"value": "11",
"type": "text"
}
]
},
"url": {
"raw": "baidu.com?a=232",
"host": [
"baidu",
"com"
],
"query": [
{
"key": "a",
"value": "232",
"description": "23232"
}
]
},
"description": "111"
},
"response": []
}
]
}
function generateUUID () {
var d = new Date().getTime()
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0
d = Math.floor(d / 16)
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16)
})
return uuid
}
// tree扁平化
function compileFlattenTree (collection) {
let keyCounnter = 0
const flatTree = []
function flattenChildren (node, parent) {
//node.nodeKey = keyCounnter++
let n = keyCounnter++
if (!node.nodeKey) {
node.nodeKey = generateUUID()
}
flatTree[n] = {
node: node,
nodeKey: node.nodeKey
}
if(typeof parent != 'undefined'){
flatTree[n].parent = parent.nodeKey
flatTree[parent.nodeKey].children.push(node.nodeKey)
}
if (node.item && node.item.length > 0) {
flatTree[n].children = []
node.item.forEach(child => flattenChildren(child, node))
}
}
if (!collection) {
return []
} else {
flattenChildren(collection)
}
return flatTree
}
let flatTree = compileFlattenTree(curCollection)
console.log(JSON.stringify(flatTree))
console