id、pid转成children格式

js版本(只能分类一级)
/** 
 * json格式转树状结构 
 * @param   {json}      json数据 
 * @param   {String}    id的字符串 
 * @param   {String}    父id的字符串 
 * @param   {String}    children的字符串 
 * @return  {Array}     数组 
 */  
function transData(a, idStr, pidStr, chindrenStr){  
    var 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++){  
        var aVal = a[j], hashVP = hash[aVal[pid]];  
        if(hashVP){  
            !hashVP[children] && (hashVP[children] = []);  
            hashVP[children].push(aVal);  
        }else{  
            r.push(aVal);  
        }  
    }  
    return r;  
}  
  
var jsonData = eval('[{"id":"4","pid":"1","name":"大家电"},{"id":"5","pid":"1","name":"生活电器"},{"id":"1","pid":"0","name":"家用电器"},{"id":"2","pid":"0","name":"服饰"},{"id":"3","pid":"0","name":"化妆"},{"id":"7","pid":"4","name":"空调"},{"id":"8","pid":"4","name":"冰箱"},{"id":"9","pid":"4","name":"洗衣机"},{"id":"10","pid":"4","name":"热水器"},{"id":"11","pid":"3","name":"面部护理"},{"id":"12","pid":"3","name":"口腔护理"},{"id":"13","pid":"2","name":"男装"},{"id":"14","pid":"2","name":"女装"},{"id":"15","pid":"7","name":"海尔空调"},{"id":"16","pid":"7","name":"美的空调"},{"id":"19","pid":"5","name":"加湿器"},{"id":"20","pid":"5","name":"电熨斗"}]');  
  
var jsonDataTree = transData(jsonData, 'id', 'pid', 'chindren');  
console.log(jsonDataTree);  
//[{"id":"1","pid":"0","name":"家用电器","chindren":[{"id":"4","pid":"1","name":"大家电","chindren":[{"id":"7","pid":"4","name":"空调","chindren":[{"id":"15","pid":"7","name":"海尔空调"},{"id":"16","pid":"7","name":"美的空调"}]},{"id":"8","pid":"4","name":"冰箱"},{"id":"9","pid":"4","name":"洗衣机"},{"id":"10","pid":"4","name":"热水器"}]},{"id":"5","pid":"1","name":"生活电器","chindren":[{"id":"19","pid":"5","name":"加湿器"},{"id":"20","pid":"5","name":"电熨斗"}]}]},{"id":"2","pid":"0","name":"服饰","chindren":[{"id":"13","pid":"2","name":"男装"},{"id":"14","pid":"2","name":"女装"}]},{"id":"3","pid":"0","name":"化妆","chindren":[{"id":"11","pid":"3","name":"面部护理"},{"id":"12","pid":"3","name":"口腔护理"}]}]
PHP版本
/**
     * Notes:
     * @param $data
     * @param int $pid       上级id
     * @param bool $ischild  是否是下级回复
     * @param int $fatherindex $arr上级id
     * @return array
     */
    public function commentSort($data ,$pid = 0, $ischild=false,$fatherindex=0)
    {
        static $arr = array() ;
        foreach ($data as $key => $value) {
            if($value['comment_pid'] == $pid){

                if($ischild){
                    //tp5框架 foreach bug
                    //下面相当于
                    //array_push($arr[$fatherindex]['child'],$value) ;
                    static $child = array() ;
                    $child[$fatherindex][] = $value ;
                    $arr[$fatherindex]['child'] = $child[$fatherindex] ;
                    $fatherindex = $fatherindex;
                } else {
                    $arr[$key] = $value ;
                    $arr[$key]['child'] = [] ;
                    $fatherindex = $key ;
                }
                //继续当前id的子类
                self::commentSort($data,$value['comment_id'],true,$fatherindex) ;
            }
        }
        return $arr ;
    }
Java版本
/**
     * @description
     * @param: list 数据集合
     * @param: res  返回的数据集合
     * @param: pid  上级id 初始0
     * @param: isChild 是否是下级回复
     * @updateTime 2020/7/31 11:20
     */
    private void buildTreeData(List<CommentDto> list,List<CommentDto> res,Integer pid,Boolean isChild){
        for (CommentDto comment:list){
            if(comment.getCommentPid() == pid){
                res.add(comment) ;
                if(!isChild){
                    res.get(res.size()-1).setChild(new ArrayList<>());
                    buildTreeData(list,res.get(res.size()-1).getChild(),comment.getCommentId(),true);
                }else{
                    buildTreeData(list,res,comment.getCommentId(),true);
                }
            }
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值