Skip to content

Instantly share code, notes, and snippets.

@Yangff

Yangff/filter.js Secret

Created September 13, 2017 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yangff/383751f551edc0caf0aef720d5c103ac to your computer and use it in GitHub Desktop.
Save Yangff/383751f551edc0caf0aef720d5c103ac to your computer and use it in GitHub Desktop.
var test_list = [
{id: "1", name: "aaa", upper: "None"},
{id: "2", name: "bbb", upper: "None"},
{id: "3", name: "ccc", upper: "None"},
{id: "4", name: "ddd", upper: "None"},
{id: "5", name: "aaaa", upper: "1"},
{id: "6", name: "aaaaa", upper: "5"},
{id: "7", name: "bbbb", upper: "2"}
]
var x = new Map(test_list.map((i) => [i.id, i]));
for (var i of test_list.filter((x) => x.upper != "None")) {
if (x.get(i.upper).children == undefined)
x.get(i.upper).children = []
x.get(i.upper).children.push(i)
}
var result_list = test_list.filter((x) => x.upper == "None")
'[{"id":"1","name":"aaa","upper":"None","children":[{"id":"5","name":"aaaa","upper":"1","children":[{"id":"6","name":"aaaaa","upper":"5"}]}]},{"id":"2","name":"bbb","upper":"None","children":[{"id":"7","name":"bbbb","upper":"2"}]},{"id":"3","name":"ccc","upper":"None"},{"id":"4","name":"ddd","upper":"None"}]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment