var w = 'a||a1||a2,b||b1,c||c1||c2,a';function start(str) { var strArr = str.split(','); var selectedArr = []; var selectedMap = {}; strArr.map(function (v) { return v.split('||'); }).forEach(function (v) { var parent; var parentCode; var child; var childCode; var childrenArr; var len = v.length; if (len === 1) { parentCode = v[0]; parent = selectedMap[parentCode]; if (!parent) { genObj(selectedMap, selectedArr, parentCode); } } else { for (var i = 1; i < len; i++) { parentCode = v[i - 1]; parent = selectedMap[parentCode]; if (!parent) { childrenArr = selectedArr; genObj(selectedMap, childrenArr, parentCode); parent = selectedMap[parentCode]; } childCode = v[i]; childrenArr = parent.children; genObj(selectedMap, childrenArr, childCode); } } }) return selectedArr;}console.log(JSON.stringify(start(w)));