遍历树

遍历树结构数据数组

 

    watch: {
            props(nv, ov) {
                console.log(nv);
                console.log(nv.inventories[0].tree,'-------------------------tree');
                if(nv.inventories.length != 0){
            //list为循环的树的数据
                    this.list = this.treeToList(nv.inventories[0].tree).reverse();
                }else{
                    this.list = [];
                }               
            },
            uuid(nv, ov) {
                // console.log(nv, '-----uuid');
                this.idcs = nv;
            }
        },
        mounted() {

        },
        methods: {
            toaddSnap() {
                this.$router.push({
                    path: "/addSnap",
                    query: {
                        id: this.idcs,
                    }
                });
            },
            treeToList(tree) {
                var queen = [];
                var out = [];
                queen = queen.concat(tree);
                while (queen.length) {
                    var first = queen.shift();
                    console.log(first.children,'--------first');
                    if (first.children) {
                        queen = queen.concat(first.children);
                        delete first["children"];
                    }
                    out.push(first);
                }
                return out;//为循环的树结构数组
            },

contact方法:ui

concat() 方法用于链接两个或多个数组。this

该方法不会改变现有的数组,而仅仅会返回被链接数组的一个副本spa

 

shift方法:code

shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。router

语法:arrayObject.shift()对象

 

children 属性返回元素的子元素的集合,是一个 HTMLCollection 对象。blog

 

reverse 颠倒数据排序排序

相关文章
相关标签/搜索