// generate name functiongenName(nameLen) { var nameStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var name = ''; for(var i = 0; i < nameLen; i++) { // get 0-25 var rand = Math.floor(Math.random()*26); name+=nameStr[rand]; } return name; }
// swap elements in arr functionswapArr(arr, i1, i2) { arr[i1] = arr.splice(i2, 1, arr[i1])[0]; return arr; }
functiongenData(count) { var objArr = [], obj = {}, rand = 0; var rootName = genName(5); var rootObj = {id: 0, name: rootName, parent: null}; objArr.push(rootObj); for (var c = 1; c < count; c++) { var parentNum = Math.floor(Math.random()*c); obj = {id: c, name: genName(5), parent: parentNum} objArr.push(obj); } // iteration function, make objArr listed by parentNum from low to high for(var m = 0; m < objArr.length - 1; m++) { for (var n = 0; n < objArr.length - m - 1; n ++) { if (objArr[n].parent > objArr[n + 1].parent ) { swapArr(objArr, n, n + 1); } } } return objArr; }