深度优先遍历多层数组对象
这个方法如果是对于下面的三级树的话可以拿到爷爷Id,自己Id,父亲Id;其实如果想要拿到label的话就把data.id换成data.label就行了
function treeFindPath(tree, func, path = []) {
? ? ? ? if (!tree) return []
? ? ? ? for (const data of tree) {
? ? ? ? ? path.push(data.id)
? ? ? ? ? if (func(data)) return path
? ? ? ? ? if (data.children) {
? ? ? ? ? ? const findChildren = treeFindPath(data.children, func, path)
? ? ? ? ? ? if (findChildren.length) return findChildren
? ? ? ? ? }
? ? ? ? ? path.pop()
? ? ? ? }
? ? ? ? return []
? ? ? }
? ? ? const i = treeFindPath(this.treeData, node => node.label === result)
比如树结构是这样的
这相当于是多可三级树
?"data": [
? ? {
? ? ? "id": "1",
? ? ? "label": "能动中心",
? ? ? "type": "1",
? ? ? "children": [
? ? ? ? {
? ? ? ? ? "id": "11",
? ? ? ? ? "label": "罐底视频",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "111",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "四高炉6道"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "112",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "西渣罐底"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "12",
? ? ? ? ? "label": "煤气柜站",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "121",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "13号道口02"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "122",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "13号道口01"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "123",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "能动中心楼顶"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "13",
? ? ? ? ? "label": "能动中心楼顶",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "131",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "44455666"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? }
? ? ? ]
? ? },
? ? {
? ? ? "id": "2",
? ? ? "label": "炼铁厂",
? ? ? "type": "1",
? ? ? "children": [
? ? ? ? {
? ? ? ? ? "id": "21",
? ? ? ? ? "label": "星云智联",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "211",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "程控楼3楼"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "212",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "程控楼1楼过道西侧"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "213",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "程控楼2楼大厅"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "214",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "公司主楼5楼西侧"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "22",
? ? ? ? ? "label": "翻车机溜车线区域",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "221",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "炼钢球罐全貌1"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "23",
? ? ? ? ? "label": "焦化化产作业区",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "231",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "4#万立储槽全貌"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "232",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "4#万立中压氧压机"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "233",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "4#万立变电所低压室"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? }
? ? ? ]
? ? },
? ? {
? ? ? "id": "3",
? ? ? "label": "炼钢厂",
? ? ? "type": "1",
? ? ? "children": [
? ? ? ? {
? ? ? ? ? "id": "31",
? ? ? ? ? "label": "熔融金属及吊运区域",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "311",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "8号吊点鞍马座"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "312",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "8号起吊点右"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "32",
? ? ? ? ? "label": "区域监控",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "321",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "测试点33"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "322",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "原料跨1"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "323",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "板坯LH钒铁柜"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "33",
? ? ? ? ? "label": "罐号识别",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "331",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "修罐间东头"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? }
? ? ? ]
? ? },
? ? {
? ? ? "id": "4",
? ? ? "label": "冷轧厂",
? ? ? "type": "1",
? ? ? "children": [
? ? ? ? {
? ? ? ? ? "id": "41",
? ? ? ? ? "label": "轧钢作业区",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "411",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "轧机主控室"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "42",
? ? ? ? ? "label": "普冷作业区",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "421",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "原料库1"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "422",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "原料库2"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "43",
? ? ? ? ? "label": "镀锌作业区",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "431",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "生产运行检测"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "432",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "轧机高压室"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "44",
? ? ? ? ? "label": "点检维护作业区",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "441",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "退火炉4"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "442",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "退火炉1"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? }
? ? ? ]
? ? }
? ]
vue遍历包含数组的对象
最近开发自己博客,在遍历对象类型数据时候,怎么也拿不到,尝试过两层遍历都不行,最终利用巧计解决了,记录下来:
请求来拿到后数据格式是下面这种
data(){
? ? return{
? ? ? noticeList:{
? ? ? ? notice:["aaaaa","bbbb","cccc"],
? ? ? ? times:[1564707990252,1564708337658,1564707990252]
? ? ? },
? ? }
? },
最终在html中这样遍历
<li v-for="(text,index) in noticeList.notice" :key="index">
? {{text}}<span>{{noticeList.times[index] | formatDate}}</span>
</li>
最重要的一点是要对象中两个数组的index对应的值是相对应的值。遍历对象中其中一个数组,另外一个数组用遍历过程中的index来获取其中对应的值,非常巧妙的一个办法。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
查看更多关于vue深度优先遍历多层数组对象方式(相当于多棵树、三级树)的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did121920