vue深度優(yōu)先遍歷多層數(shù)組對(duì)象方式(相當(dāng)于多棵樹、三級(jí)樹)
深度優(yōu)先遍歷多層數(shù)組對(duì)象
這個(gè)方法如果是對(duì)于下面的三級(jí)樹的話可以拿到爺爺Id,自己Id,父親Id;其實(shí)如果想要拿到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)比如樹結(jié)構(gòu)是這樣的
這相當(dāng)于是多可三級(jí)樹
?"data": [
? ? {
? ? ? "id": "1",
? ? ? "label": "能動(dòng)中心",
? ? ? "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號(hào)道口02"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "122",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "13號(hào)道口01"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "123",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "能動(dòng)中心樓頂"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "13",
? ? ? ? ? "label": "能動(dòng)中心樓頂",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "131",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "44455666"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? }
? ? ? ]
? ? },
? ? {
? ? ? "id": "2",
? ? ? "label": "煉鐵廠",
? ? ? "type": "1",
? ? ? "children": [
? ? ? ? {
? ? ? ? ? "id": "21",
? ? ? ? ? "label": "星云智聯(lián)",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "211",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "程控樓3樓"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "212",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "程控樓1樓過道西側(cè)"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "213",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "程控樓2樓大廳"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "214",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "公司主樓5樓西側(cè)"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "22",
? ? ? ? ? "label": "翻車機(jī)溜車線區(qū)域",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "221",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "煉鋼球罐全貌1"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "23",
? ? ? ? ? "label": "焦化化產(chǎn)作業(yè)區(qū)",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "231",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "4#萬立儲(chǔ)槽全貌"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "232",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "4#萬立中壓氧壓機(jī)"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "233",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "4#萬立變電所低壓室"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? }
? ? ? ]
? ? },
? ? {
? ? ? "id": "3",
? ? ? "label": "煉鋼廠",
? ? ? "type": "1",
? ? ? "children": [
? ? ? ? {
? ? ? ? ? "id": "31",
? ? ? ? ? "label": "熔融金屬及吊運(yùn)區(qū)域",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "311",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "8號(hào)吊點(diǎn)鞍馬座"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "312",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "8號(hào)起吊點(diǎn)右"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "32",
? ? ? ? ? "label": "區(qū)域監(jiān)控",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "321",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "測(cè)試點(diǎn)33"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "322",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "原料跨1"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "323",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "板坯LH釩鐵柜"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "33",
? ? ? ? ? "label": "罐號(hào)識(shí)別",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "331",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "修罐間東頭"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? }
? ? ? ]
? ? },
? ? {
? ? ? "id": "4",
? ? ? "label": "冷軋廠",
? ? ? "type": "1",
? ? ? "children": [
? ? ? ? {
? ? ? ? ? "id": "41",
? ? ? ? ? "label": "軋鋼作業(yè)區(qū)",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "411",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "軋機(jī)主控室"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "42",
? ? ? ? ? "label": "普冷作業(yè)區(qū)",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "421",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "原料庫(kù)1"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "422",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "原料庫(kù)2"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "43",
? ? ? ? ? "label": "鍍鋅作業(yè)區(qū)",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "431",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "生產(chǎn)運(yùn)行檢測(cè)"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "432",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "軋機(jī)高壓室"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? "id": "44",
? ? ? ? ? "label": "點(diǎn)檢維護(hù)作業(yè)區(qū)",
? ? ? ? ? "type": "2",
? ? ? ? ? "children": [
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "441",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "退火爐4"
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? "id": "442",
? ? ? ? ? ? ? "type": "3",
? ? ? ? ? ? ? "label": "退火爐1"
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? }
? ? ? ]
? ? }
? ]vue遍歷包含數(shù)組的對(duì)象
最近開發(fā)自己博客,在遍歷對(duì)象類型數(shù)據(jù)時(shí)候,怎么也拿不到,嘗試過兩層遍歷都不行,最終利用巧計(jì)解決了,記錄下來:
請(qǐng)求來拿到后數(shù)據(jù)格式是下面這種
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>最重要的一點(diǎn)是要對(duì)象中兩個(gè)數(shù)組的index對(duì)應(yīng)的值是相對(duì)應(yīng)的值。遍歷對(duì)象中其中一個(gè)數(shù)組,另外一個(gè)數(shù)組用遍歷過程中的index來獲取其中對(duì)應(yīng)的值,非常巧妙的一個(gè)辦法。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue flex 布局實(shí)現(xiàn)div均分自動(dòng)換行的示例代碼
這篇文章主要介紹了vue flex 布局實(shí)現(xiàn)div均分自動(dòng)換行,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
vuex中...mapstate和...mapgetters的區(qū)別及說明
這篇文章主要介紹了vuex中...mapstate和...mapgetters的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue-Element-Admin集成自己的接口實(shí)現(xiàn)登錄跳轉(zhuǎn)
關(guān)于這個(gè)Vue-element-admin中的流程可能對(duì)于新的同學(xué)不是很友好,所以本文將結(jié)合實(shí)例代碼,介紹Vue-Element-Admin集成自己的接口實(shí)現(xiàn)登錄跳轉(zhuǎn),感興趣的小伙伴們可以參考一下2021-06-06
vue3+vite+vant項(xiàng)目下按需引入vant報(bào)錯(cuò)Failed?to?resolve?import的原因及解決
這篇文章主要給大家介紹了關(guān)于vue3+vite+vant項(xiàng)目下按需引入vant報(bào)錯(cuò)Failed?to?resolve?import的原因及解決方案,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
詳解在vue-cli中使用graphql即vue-apollo的用法
這篇文章主要介紹了詳解在vue-cli中使用graphql即vue-apollo的用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09

