Created
May 22, 2021 06:14
-
-
Save ufo22940268/30e0ef5bd01c6b358e90e077360811a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const obj = [{ | |
pid: 'p1', | |
bid: 'b1', | |
qid: 'q1', | |
}, { | |
pid: 'p1', | |
bid: 'b2', | |
qid: 'q2', | |
}, { | |
pid: 'p1', | |
bid: 'b2', | |
qid: 'q3', | |
}, { | |
pid: 'p4', | |
bid: 'b4', | |
qid: 'q4', | |
}]; | |
let ret = []; | |
for (let e of obj) { | |
let pi = ret.findIndex(t => t.pid == e.pid); | |
if (pi == -1) { | |
ret.push({pid: e.pid}); | |
pi = ret.length - 1; | |
} | |
const po = ret[pi]; | |
if (!po.bos) { | |
po.bos = []; | |
} | |
let bi = po.bos.findIndex(t => t.bid == e.bid); | |
if (bi == -1) { | |
po.bos.push({bid: e.bid}); | |
bi = po.bos.length - 1; | |
} | |
const bo = po.bos[bi]; | |
if (!bo.qids) { | |
bo.qids = []; | |
} | |
bo.qids.push(e); | |
} | |
console.log(`ret: ` + JSON.stringify(ret, null, 4) + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment