文件树的功能整理

我的需求是实现一个文件树,需要对原始数据结构进行处理,返回前端需要的数据。

1、mongodb数据库中存放的原始数据:

let fData =
[{"pid": null,"_id": "5e847c7f11228f1e88095dda","name": "公共资源"},{"pid": "5e847c7f11228f1e88095dda","_id": "5e848a02b9d8b326ea4e2e0a","name": "文件夹一"},{"pid": "5e847c7f11228f1e88095dda","_id": "5e8889473a9edb4235d98bae","name": "文件夹二"},{"pid": "5e847c7f11228f1e88095dda","_id": "5e8889493a9edb4235d98baf","name": "文件夹三"},{"pid": "5e8889493a9edb4235d98baf","_id": "5e8889563a9edb4235d98bb0","name": "档案"},{"pid": "5e8889493a9edb4235d98baf","_id": "5e8889663a9edb4235d98bb1","name": "音频"},{"pid": "5e8889493a9edb4235d98baf","_id": "5e88896c3a9edb4235d98bb2","name": "项目部"}
]

2、需要将其转化为层级数据。

const getTreeData = (nodes) => {nodes.forEach((ele: any) => {// 名称ele.title = ele.nameele.expand = trueele.mode = 'show'delete ele.namelet pid = ele.pid;if (pid === null) {// 是根元素的话 ,不做任何操作,如果是正常的for-i循环,可以直接continue.} else {// 如果ele是子元素的话 ,把ele扔到他的父亲的child数组中.nodes.forEach(d => {if (d._id == pid) {let childArray = d.children;if (!childArray) {childArray = [];}childArray.push(ele);d.children = childArray;}});}});// 去除重复元素return nodes.filter(ele => ele.pid === null);
}
const tData = getTreeData(fOnes);

执行完之后,tData为:

[{"pid": null,"_id": "5e847c7f11228f1e88095dda","title": "公共资源","expand": true,"mode": "show","children": [{"pid": "5e847c7f11228f1e88095dda","_id": "5e848a02b9d8b326ea4e2e0a","title": "文件夹一","expand": true,"mode": "show"},{"pid": "5e847c7f11228f1e88095dda","_id": "5e8889473a9edb4235d98bae","title": "文件夹二","expand": true,"mode": "show"},{"pid": "5e847c7f11228f1e88095dda","_id": "5e8889493a9edb4235d98baf","title": "文件夹三","expand": true,"mode": "show","children": [{"pid": "5e8889493a9edb4235d98baf","_id": "5e8889563a9edb4235d98bb0","title": "档案","expand": true,"mode": "show"},{"pid": "5e8889493a9edb4235d98baf","_id": "5e8889663a9edb4235d98bb1","title": "音频","expand": true,"mode": "show"},{"pid": "5e8889493a9edb4235d98baf","_id": "5e88896c3a9edb4235d98bb2","title": "项目部","expand": true,"mode": "show"}]}]}
]

3、根据层级数据和一个子结点,获取父节点信息

 const folder_id = '5e8889493a9edb4235d98baf';// 定义数组,用于存放let arr = [];const getParentsIds = (data) => {for (let i = 0; i < data.length; i++) {let temp = data[i]if (temp._id == folder_id) {arr.push(temp._id);return 1}if (temp && temp.children && temp.children.length > 0) {let t = getParentsIds(temp.children)if (t == 1) {arr.push(temp._id)return 1}}}}getParentsIds(tData)

输出的arr

[ '5e8889493a9edb4235d98baf', '5e847c7f11228f1e88095dda' ]

4、根据原始数据和一个子结点,获取父节点信息

const fnode = {"pid": "5e8889493a9edb4235d98baf","_id": "5e8889563a9edb4235d98bb0","title": "档案","expand": true,"mode": "show"
};
const findP = (nodes, node) => {const ans = [];for (let i = 0; i < nodes.length; i++) {if (node.pid == nodes[i]._id) {ans.push(nodes[i]);return ans.concat(findP(nodes, nodes[i]));}}
}
const pOnes = findP(fOnes, fnode).filter(x => {return x});

返回结果如下:

[{"pid": "5e847c7f11228f1e88095dda","_id": "5e8889493a9edb4235d98baf","name": "文件夹三"},{"pid": null,"_id": "5e847c7f11228f1e88095dda","name": "公共资源"}
]

5、根据原始数据和一个子结点,获取这个子结点下的子树

const fnode = {"pid": "5e847c7f11228f1e88095dda","_id": "5e8889493a9edb4235d98baf","title": "文件夹三"
};
//找子节点
const findC = (nodes, node) => {let ans = [];for (let i = 0; i < nodes.length; i++) {if (node._id == nodes[i].pid) {ans.push(nodes[i]);ans = ans.concat(findC(nodes, nodes[i]));}}return ans;
}
const cOnes = findC(fOnes, fnode);

此时cOnes中数据为

[{"pid": "5e8889493a9edb4235d98baf","_id": "5e8889563a9edb4235d98bb0","name": "档案"},{"pid": "5e8889493a9edb4235d98baf","_id": "5e8889663a9edb4235d98bb1","name": "音频"},{"pid": "5e8889493a9edb4235d98baf","_id": "5e88896c3a9edb4235d98bb2","name": "项目部"}
]

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415201.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JavaScript的基本语法

1.JavaScript中的表示符合保留关键字&#xff1a;JavaScript中定义的符号必须以字母&#xff0c;下划线_或美元符$开始&#xff0c;其他字符可以是字母数字&#xff0c;下划线或者美元符。如变量名&#xff0c;函数名等。但是&#xff0c;标识符不能是JavaScript中的保留关键字…

Android 视频播放器,VideoView播放视频

实现demo&#xff1a;https://download.csdn.net/download/meixi_android/13729352 获取视频时长 delyedTime videoView.getDuration();//单位毫秒&#xff08;ms&#xff09; 引入视频模块 implementation project(:dkplayer-java) implementation project(:dkplayer-ui)视…

工作174:数组转换为对象项目案例

/* getAction("/task",).then(res>{console.log(res)let List[]res.data.items.map((value,index)>{/!* console.log(value.task_recode)*!/List.push({...value.task_recode})})this.tableDataListconsole.log(this.tableData)})*/ 本次直接处理 转换为数…

JDK8和JDK1.8有何区别

通常所说的JDK8和JDK1.8是同一个意思。

【NOIP 2017】列队

Description Sylvia 是一个热爱学习的女♂孩子。 前段时间&#xff0c;Sylvia 参加了学校的军训。众所周知&#xff0c;军训的时候需要站方阵。 Sylvia 所在的方阵中有nm名学生&#xff0c;方阵的行数为 n&#xff0c;列数为 m。 为了便于管理&#xff0c;教官在训练开始时&…

AAPT2 error: check logs for details.

/1、全部替代你的项目build.gradle内容&#xff1a; // Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {repositories { // maven { // url https://maven.google.com // }mavenCe…

工作175:数据在表格横坐标动态显示

1数据格式 2对数据进行处理 created() {getAction("/task/arrange").then(res>{console.log(res)this.tableDatares.data.itemsthis.timeres.data.timeconsole.log(this.time)res.data.time.map((value,index)>{console.log(value)let arr {prop:,label:value.…

mac安装mysql记录,使用zsh

1、正常安装去官网下载最新版&#xff0c;依次下一步就可以。 2、配置zsh 执行vim ~/.zshrc_profil 添加mysql/bin的目录&#xff1a; export PATH$PATH:/usr/local/mysql/bin添加完成后:wq保存 最后在命令行输入source ~/.zshrc_profile&#xff0c;执行脚本。 3、连接数…

vue中的ES6语法整理1

1、箭头函数 ES6允许使用“箭头”&#xff08;>&#xff09;定义函数 var f v > v;//等价于var f function(v){return v; }; 如果箭头函数不需要参数&#xff0c;则定义如下&#xff1a; var f () > 5; // 等同于 var f function () { return 5 }; 如果箭头函…

Android 生成二维码,条形码,二维码添加logo

zxing生成二维码 implementation com.google.zxing:core:3.3.1 implementation(name: zxing-1.0.1, ext: aar) implementation com.github.bumptech.glide:glide:4.9.0 annotationProcessor com.github.bumptech.glide:compiler:4.9.0 private Bitmap getCodeBitmap(String c…

java setDataSource 报红

开始学习spring security遇到一个问题&#xff0c;setDataSource老是报红 解决方案&#xff0c;在pom.xml中增加 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId><version>2…

Android 识别图片二维码,以及设置状态栏颜色

zxing依赖&#xff1a;compile cn.yipianfengye.android:zxing-library:2.2 初始化&#xff1a;private String SAVE_PIC_PATH Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)? Environment.getExternalStorageDirectory().getAbsolute…

linux之sed

sed 是一个流编辑器(stream editor)&#xff0c;主要用来执行文本替换。但 sed 的主要设计目的是以批处理的方式而不是交互的方式来编辑文件。 命令简介 基本命令格式 sed [常用选项] 命令文本 输入 常用选项 -n (--quiet, --silent)&#xff1a;安静模式。在 sed 的基本用法中…

mac 升级 15.4之后,部分软件无法打开解决

我的mac升级版本后&#xff0c;有一些好用的软件就打不开了&#xff0c;比如orc识别软件text scanner 解决方法&#xff1a; sudo codesign -f -s - --deep /Applications/TextScan.app可以直接输入 sudo codesign -f -s - --deep 再把文件拖拽过去即可

Android 截图,截取指定view截图

二、具体实现方式 实用截图方法截取整个activity public static Bitmap shotActivity(Activity ctx) {View view ctx.getWindow().getDecorView();view.setDrawingCacheEnabled(true);view.buildDrawingCache();Bitmap bp Bitmap.createBitmap(view.getDrawingCache(), 0, 0…

java使用AntPathMatcher进行uri匹配

需求&#xff1a;我在做rbac权限校验的时候&#xff0c;设置管理员的访问路径为/admin/**,希望所有的开头为/admin/的uri操作地址都能进行匹配判断。 import org.springframework.util.AntPathMatcher;String content "/admin/acuff"; String pattern "/admi…

百度经验 回享计划

https://jingyan.baidu.com/user/income 转载于:https://www.cnblogs.com/qdrs/p/7940353.html