js返回树形结构数据

/**
 * 树形结构转换
 * @param a
 * @param idStr
 * @param pidStr
 * @param chindrenStr
 * @returns {Array}
 */
function transData(a, idStr, pidStr, chindrenStr){
    let r = [], hash = {}, id = idStr, pid = pidStr, children = chindrenStr, i = 0, j = 0, len = a.length;
    for(; i < len; i++){
        hash[a[i][id]] = a[i];
    }
    for(; j < len; j++){
        let aVal = a[j], hashVP = hash[aVal[pid]];      //当前对象&pid对象
        if(hashVP){
            !hashVP[children] && (hashVP[children] = []);
            hashVP[children].push(aVal);
        }else{
            r.push(aVal);
        }
    }
    return r;
}

 js返回树形结构数据_第1张图片

转载于:https://my.oschina.net/caiya928/blog/784098

你可能感兴趣的:(js返回树形结构数据)