Last active
September 18, 2020 03:18
-
-
Save M4R14/b73b96eb44d827fae8bcf4e17ff7be4c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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