Skip to content

Instantly share code, notes, and snippets.

@M4R14
Last active September 18, 2020 03:18
Show Gist options
  • Select an option

  • Save M4R14/b73b96eb44d827fae8bcf4e17ff7be4c to your computer and use it in GitHub Desktop.

Select an option

Save M4R14/b73b96eb44d827fae8bcf4e17ff7be4c to your computer and use it in GitHub Desktop.
const data = [
{ id: 1, name: '1111', ref: 2 },
{ id: 2, name: '222', ref: 3 },
{ id: 3, name: '3333', ref: 0 }
];
const getOne = (_id) => data.find(item => item.id == _id)
const render = (it) => {
let text = [it.name]
if (it.ref > 0) {
let ref = getOne(it.ref)
while(ref) {
text.push(ref.name)
ref = getOne(ref.ref)
}
}
return text.join('/')
}
data.forEach(it => console.log(render(it)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment