/** * 递归函数 * @param setValues 树型数据 * @param valueId 查找的id */ function recursionGet(setValues, valueId) { let value = null; for (let index = 0; index < setValues.length; index += 1) { if (setValues[index].value === valueId) { value = setValues[index].label; break; } if (setValues[index].children instanceof Array && setValues[index].children.length > 0) { const text = recursionGet(setValues[index].children, valueId); if (text) return text; } } return value; } // 调用 const data =recursionGet(list,301) console.log(data) // 输出 行政 用vue写出来
时间: 2024-04-07 19:32:14 浏览: 50
<template>
<div>
<span>{{ department }}</span>
</div>
</template>
<script>
export default {
name: "DepartmentSelector",
props: {
list: {
type: Array,
required: true
},
valueId: {
type: Number,
required: true
}
},
data() {
return {
department: null
};
},
mounted() {
this.department = this.recursionGet(this.list, this.valueId);
},
methods: {
recursionGet(setValues, valueId) {
let value = null;
for (let index = 0; index < setValues.length; index += 1) {
if (setValues[index].value === valueId) {
value = setValues[index].label;
break;
}
if (
setValues[index].children instanceof Array &&
setValues[index].children.length > 0
) {
const text = this.recursionGet(
setValues[index].children,
valueId
);
if (text) return text;
}
}
return value;
}
}
};
</script>
阅读全文