javascript 无限分类
* 根据php无限分类实现js版本的
/**
* 根节点 parentid=0, 每个节点都有id, parentid字段
* @param items
* @returns {*}
*/
function getTree(items) {
"use strict";
var children = new Map();
items.forEach(function(item){
if (children.has(item.parentid)) {
children.get(item.parentid).push(item);
} else {
children.set(item.parentid, [item]);
}
});
items.forEach(function(item) {
if (children.has(item.id)) {
item.children = children.get(item.id);
}
});
return children.get(0);
}
* test
var items = [{
"id": 4,
"parentid": 2,
"name": "Laravel"
}, {
"id": 1,
"parentid": 0,
"name": "PHP"
}, {
"id": 43,
"parentid": 42,
"name": "PHPStorm"
}, {
"id": 44,
"parentid": 42,
"name": "EclipsePDT"
}, {
"id": 2,
"parentid": 1,
"name": "PHP_Framework"
}, {
"id": 3,
"parentid": 2,
"name": "ThinkPHP5"
}, {
"id": 42,
"parentid": 1,
"name": "DevTools"
}];
var c = getTree(items);
// console.log(c);
console.log(JSON.stringify(c));
output:
[{"id":1,"parentid":0,"name":"PHP","children":[{"id":2,"parentid":1,"name":"PHP_Framework","children":[{"id":4,"parentid":2,"name":"Laravel"},{"id":3,"parentid":2,"name":"ThinkPHP5"}]},{"id":42,"parentid":1,"name":"DevTools","children":[{"id":43,"parentid":42,"name":"PHPStorm"},{"id":44,"parentid":42,"name":"EclipsePDT"}]}]}]
* 在线json校验格式化输出
http://www.bejson.com/
javascript 无限分类的更多相关文章
- 后台树状菜单,js实现递归无限分类
//新闻类别管理 public function new_classify() { $arr = M('news_classify')->where("fid = 0")-& ...
- php无限分类方法类
创建数据库以及表 CREATE DATABASE `sortclass`DEFAULT CHARSET utf8; CREATE TABLE IF NOT EXISTS `class` ( `cid` ...
- MySql无限分类数据结构--预排序遍历树算法
MySql无限分类数据结构--预排序遍历树算法 无限分类是我们开发中非常常见的应用,像论坛的的版块,CMS的类别,应用的地方特别多. 我们最常见最简单的方法就是在MySql里ID ,parentID, ...
- TreeView递归绑定无限分类数据
TreeView递归绑定无限分类数据 实现一个动态绑定,无限级分类数据时,需要将数据绑定到TreeView控件,分类表的结构是这样的: 字段 类型 Id int ParentId int Name N ...
- 关于无限分类的树状输出(id,name,pid)类型的
首先创建无限分类的数据表,我这里采用的是id.name.pid这种类型(当然还有很多种无限分类的方式了,比如:id.name.pid.path.left.right左右节点的形式) CREATE TA ...
- php递归方法实现无限分类实例
数组: 代码如下 复制代码 $items = array( array('id' => 1, 'pid' => 0, 'name' => '一级11' ), array('id' ...
- PHP+Mysql无限分类的方法汇总
无限分类是个老话题了,来看看PHP结合Mysql如何实现.第一种方法这种方法是很常见.很传统的一种,先看表结构表:categoryid int 主键,自增name varchar 分类名称pid in ...
- PHP全路径无限分类导航LINK代码实现
<?php /** * @param php全路径无限分类 */ include('db.inc.php'); function getPathCate($cateid){ $sql = &qu ...
- PHP全路径无限分类原理
全路径无限分类:以一个字段把他所有的父级id按顺序记录下来以此实现的无限分类叫做全路径无限分类 优点:查询方便 缺点:增加,移动分类时数据维护时稍微复杂.
随机推荐
- NOIP 模拟 $34\; \rm Merchant$
题解 \(by\;zj\varphi\) 对于选的物品,总值一定有在前一段区间递减,后一段递增的性质,那么就可以二分. check()时只递归归并大的一段,用nth_element即可 Code #i ...
- 题解 Cover
传送门 考场上坚持认为树上背包可以有70pts,于是爆零了 首先我以为是树上背包的部分分其实是树形DP 然后极其魔鬼的正解: 首先我们令 \(dp[i][j]\) 为以i为根的子树覆盖次数至多为j时的 ...
- docker harbor安装
# 官网下载离线包,https://github.com/goharbor/harbor/releases src]# tar xf harbor-offline-installer-v1.8.3.t ...
- jsoup的Document类
一.简介 Document是一个装载html的文档类,它是jsoup一个非常重要的类.类声明:public class Document extends Element .Document是Node间 ...
- C# 简单的对称加密
const string KEY_64 = "HuidTeac";//注意了,是8个字符 const string IV_64 = "HuidTeac"; pu ...
- 关于对String中intern方法的理解
在java的String中有个一直被我们忽视了的方法intern方法:它的官方解释是:一个初始时为空的字符串池,它由类 String 私有地维护. 当调用 intern 方法时,如果池已经包含一个等于 ...
- go语言初始化结构体指针
go语言初始化结构体指针 head:=&ListNode{} 或者 head:=new(ListNode)
- python使用pip安装模块出错 Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None))
python使用pip安装模块出错 Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) 问题: ...
- python opencv cv2 imshow threading 多线程
除了线程同步,还需要注意的是「窗口处理」要放在主线程 #!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys import threadin ...
- Sentry For React 完整接入详解(2021 Sentry v21.8.x)前方高能预警!三万字,慎入!
内容源于:https://docs.sentry.io/platforms/javascript/guides/react/ 系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - ...