Skip to content

Instantly share code, notes, and snippets.

@ufo22940268
Created May 22, 2021 06:14
Show Gist options
  • Save ufo22940268/30e0ef5bd01c6b358e90e077360811a9 to your computer and use it in GitHub Desktop.
Save ufo22940268/30e0ef5bd01c6b358e90e077360811a9 to your computer and use it in GitHub Desktop.
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