已知子元素id,怎么获取它所有的父元素?
用递归来实现
/*** 查找匹配的完整路径* id: 匹配的值* data: 匹配的数组数据* prop: 匹配的字段名*/getFathersById (id, data, prop) {var arrRes = []const rev = (data, nodeId) => {for (var i = 0, length = data.length; i < length; i++) {const node = data[i]if (node[prop] === nodeId) {arrRes.unshift(node[prop])return true} else {if (node.child && node.child.length) {if (rev(node.child, nodeId)) {arrRes.unshift(node[prop])return true}}}}return false}rev(data, id)return arrRes}
使用方法:
allCates = this.getFathersById(item.categoryId, this.categoryOptions, \'id\')