<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
const city = [{
companyId: 1,
id: "1",
label: "四川省",
parentId: "",
value: "1",
children: [{
companyId: 1,
id: "2",
label: "成都市",
parentId: "1",
value: "2",
children: [{
companyId: 1,
id: "3",
label: "高新區(qū)",
parentId: "2",
value: "3",
children: [{
companyId: 1,
id: "4",
label: "環(huán)球中心",
parentId: "3",
value: "4"
}]
}]
}]
}]
const city2 = [
{
companyId: 1,
id: "0",
label: "江西省",
parentId: "-99",
value: "0",
},
{
companyId: 1,
id: "1",
label: "四川省",
parentId: "-99",
value: "1",
},
{
companyId: 1,
id: "2",
label: "成都市",
parentId: "1",
value: "2",
},
{
companyId: 1,
id: "3",
label: "高新區(qū)",
parentId: "2",
value: "3",
},
{
companyId: 1,
id: "4",
label: "環(huán)球中心",
parentId: "3",
value: "4"
}
]
// 扁平數(shù)據(jù)轉(zhuǎn)換樹狀數(shù)據(jù)
function buildTree(list, rootID = "0") {
let root = null;
if (list && list.length) {
root = {
id: rootID,
parentId: null,
children: []
};
const group = {};
for (let index = 0; index < list.length; index += 1) {
if (list[index].parentId !== null && list[index].parentId !== undefined) {
if (!group[list[index].parentId]) {
group[list[index].parentId] = [];
}
group[list[index].parentId].push(list[index]);
}
}
const queue = [];
queue.push(root);
while (queue.length) {
const node = queue.shift();
node.children = group[node.id] && group[node.id].length ? group[node.id] : [];
if (node.children) {
Array.prototype.push.apply(queue, node.children);
}
}
}
return root;
}
// 通過id找父id(包括自己)
function getParentIdList(array, id) {
let parentArray = [];
if (array.length === 0) {
return parentArray;
}
const recursion = function (arrayNew, id) {
for (let i = 0; i < arrayNew.length; i++) {
let node = arrayNew[i];
if (node.id === id) {
parentArray.unshift(id);
recursion(array, node.parentId);
break;
} else {
if (!!node.children) {
recursion(node.children, id);
}
}
}
return parentArray;
}
let arrayNew = array;
parentArray = recursion(arrayNew, id);
return parentArray;
}
let arr = buildTree(JSON.parse(JSON.stringify(city2)), '-99')
console.log('arr', arr)
console.log('getParentIdList', getParentIdList(arr.children, '3'))
</script>
</html>
JS數(shù)組對象:通過子id找所有父id
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門焰雕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來衷笋,“玉大人,你說我怎么就攤上這事矩屁”僮冢” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵吝秕,是天一觀的道長泊脐。 經(jīng)常有香客問我,道長烁峭,這世上最難降的妖魔是什么容客? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上耘柱,老公的妹妹穿的比我還像新娘如捅。我一直安慰自己,他們只是感情好调煎,可當(dāng)我...
- 文/花漫 我一把揭開白布镜遣。 她就那樣靜靜地躺著,像睡著了一般士袄。 火紅的嫁衣襯著肌膚如雪悲关。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼这敬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蕉朵?” 一聲冷哼從身側(cè)響起崔涂,我...
- 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎始衅,沒想到半個(gè)月后冷蚂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡汛闸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年蝙茶,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蛉拙。...
- 正文 年R本政府宣布畸肆,位于F島的核電站宦芦,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏轴脐。R本人自食惡果不足惜调卑,卻給世界環(huán)境...
- 文/蒙蒙 一抡砂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧恬涧,春花似錦注益、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至提揍,卻和暖如春啤月,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背劳跃。 一陣腳步聲響...
- 正文 我出身青樓郑诺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親贸人。 傳聞我的和親對象是個(gè)殘疾皇子间景,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- myFilter(arr1,arr2) { return arr1.filter((ele) => ...
- 下面選了最近十年里,十位名人所做的畢業(yè)演講曹铃。那么多的故事與經(jīng)歷缰趋,其實(shí)只想告訴你一件事: 面對迷茫和不確定的未來,我...
- tree組件 通過一個(gè)id 去查詢其所有上級數(shù)據(jù)并返回一個(gè)數(shù)組1陕见、前提是父級和子級數(shù)據(jù)有關(guān)聯(lián)2秘血、本代碼實(shí)現(xiàn)是使用t...