json简单格式转树状结构 工具方法

来源:互联网 发布:广告公司用到的软件 编辑:程序博客网 时间:2024/06/09 21:23
/** * json简单格式转树状结构 工具方法 *  * @param jsondata *            json数据 * @param idStr *            id的字符串 * @param pidStr *            父id的字符串 * @param childrenStr *            children的字符串 * @return {Array} 数组 */function transData(jsondata, idStr, pidStr, childrenStr) { var r = [], hash = {}, id = idStr, pid = pidStr, children = childrenStr, i = 0, j = 0, len = jsondata.length;    for(; i < len; i++){            hash[jsondata[i][id]] = jsondata[i];        }          for(; j < len; j++){            var aVal = jsondata[j], hashVP = hash[aVal[pid]];            if(hashVP){         !hashVP[children] && (hashVP[children] = []);               hashVP[children].push(aVal);            }else{                r.push(aVal);            }        }        return r;    }

0 0
原创粉丝点击