已知 子節(jié)點 求父節(jié)點
例如 已知 AAA
希望 得到 A-AA-AAA的格式
data3: [{
id: 1,
label: '一級 2',
children: [{
id: 3,
label: '二級 2-1',
children: [{
id: 4,
label: '三級 3-1-1'
}, {
id: 5,
label: '三級 3-1-2',
disabled: true
}]
}, {
id: 2,
label: '二級 2-2',
disabled: true,
children: [{
id: 6,
label: '三級 3-2-1'
}, {
id: 7,
label: '三級 3-2-2',
disabled: true
}]
}]
}],上次遇到這個問題,估計和你的一樣,問了問后端大神,給我解決了,可是我不知道原理是什么。給你貼出代碼,你看看吧。
<script>
let treeData = [
{
id: 1,
sub: [
{
id: 2,
sub: [
{
id: 12,
sub: [
{
id: 13
},
{
id: 14
}
]
},
{
id: 3,
sub: [
{
id: 4,
},
{
id: 9,
sub: [
{
id: 10,
},
{
id: 11
}
]
}
]
},
{
id: 7,
sub: [
{id: 8},
]
}
]
},
{
id: 5,
sub: [
{id: 6},
]
}
]
},
{
id: 100
}
]
let currentId = 10 // 當前id
let arr = [] // 定義數(shù)組,存放當前id的直系父ids
function getParentsIds(data) {
for (let i = 0; i < data.length; i++) {
let temp = data[i]
if (temp.id == currentId) {
arr.push(temp.id);
return 1
}
if (temp && temp.sub && temp.sub.length > 0) {
let t = getParentsIds(temp.sub)
if (t == 1) {
arr.push(temp.id)
return 1
}
}
}
}
getParentsIds(treeData)
console.log(arr)
</script>目測lz遇到的問題是樹形選擇器,某個節(jié)點被選中后希望拿到該節(jié)點的層級信息,目前找到比較好的方案是拿到數(shù)據(jù)后遍歷一遍,在每一個節(jié)點上生成一個levelInfo字段,標識當前層級信息。
[{
projectid: 110000,
name: "一級 1",
levelInfo: "110000"
children: [{
projectid: 110100,
name: "二級 1-1",
levelInfo: "110000-110100"
children: [{
projectid: 110101,
name: "三級 1-1-1",
children: null,
levelInfo: "110000-110100-110101"
}, {
projectid: 110102,
name: "三級 1-1-2",
children: null,
levelInfo: "110000-110100-110102"
}]
}, {
projectid: 110200,
name: "二級 1-2",
levelInfo: "110000-110200"
children: [{
projectid: 110201,
name: "三級 1-2-1",
children: null,
levelInfo: "110000-110200-110201"
}, {
projectid: 110202,
name: "三級 1-2-2",
children: null,
levelInfo: "110000-110200-110202"
}]
}]
}]
// 生成該結(jié)構(gòu)的函數(shù),希望可以幫到你
function formatTree(arr, levelInfo = '') {
return arr.map(item => {
const newParent = levelInfo ? levelInfo + '-' + item.projectid : '' + item.projectid;
const temp = {
...item,
levelInfo: newParent
};
if (item.children) {
temp.children = formatTree(item.children, newParent);
}
return temp;
})
}let find = (array, label) =>{
let stack = [];
let going = true;
let walker = (array, label) => {
array.forEach(item => {
if (!going) return;
stack.push(item['label']);
if (item['label'] === label) {
going = false;
} else if (item['children']) {
walker(item['children'], label);
} else {
stack.pop();
}
});
if (going) stack.pop();
}
walker(array, label);
return stack.join('-');
}
console.log(find(data, '三級 3-2-2'))
// 一級 2-二級 2-2-三級 3-2-2
應該是 DFS
const getParent = (data, target) => {
const get = (children, target, record = []) => (
children.reduce((result, { label, children: innerChildren }) => {
if (label === target) {
return [...record, target]
}
if (innerChildren) {
return [...result, ...get(innerChildren, target, [...record, label])]
}
return result
}, []))
return get(data, target).join('-')
}
// 一級 2-二級 2-1-三級 3-1-2
const str = getParent(data, '三級 3-1-2')北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內(nèi)教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學校辦產(chǎn)業(yè)為響應國家深化產(chǎn)教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復興的升級產(chǎn)業(yè)鏈。利用北京大學優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓領(lǐng)域的先行者
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。