第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在類型腳本和角度中對(duì)父項(xiàng)和子項(xiàng)進(jìn)行排序

在類型腳本和角度中對(duì)父項(xiàng)和子項(xiàng)進(jìn)行排序

慕桂英3389331 2022-09-29 16:47:11
我有這個(gè)列表:0: {id: 7, name: "333", code: "333", type: 3, hasParent: true, parentId: 4}1: {id: 6, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: false, parentId: null}2: {id: 5, name: "111", code: "111", type: 3, hasParent: true, parentId: 4}3: {id: 4, name: "22", code: "22", type: 1, hasParent: false, parentId: null}4: {id: 3, name: "yyy", code: "yyyy", type: 2, hasParent: false, parentId: null}5: {id: 2, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: true, parentId: 1}6: {id: 1, name: "cbcvb", code: "cvbcvcbv", type: 2, hasParent: false, parentId: null}我需要按父級(jí)和子級(jí)對(duì)此列表進(jìn)行排序。如果項(xiàng)目的父值等于另一個(gè)項(xiàng)目的 id 的值,則應(yīng)將具有 parentId 值的項(xiàng)目放在父值等于 id 值的項(xiàng)目下。喜歡這個(gè)列表:4: {id: 3, name: "yyy", code: "yyyy", type: 2, hasParent: false, parentId: 6}1: {id: 6, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: false, parentId: null}0: {id: 7, name: "333", code: "333", type: 3, hasParent: true, parentId: 4}2: {id: 5, name: "111", code: "111", type: 3, hasParent: true, parentId: 4}3: {id: 4, name: "22", code: "22", type: 1, hasParent: false, parentId: null}5: {id: 2, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: true, parentId: 1}6: {id: 1, name: "cbcvb", code: "cvbcvcbv", type: 2, hasParent: false, parentId: null}我寫了這段代碼,但它不起作用,沒(méi)有對(duì)項(xiàng)目列表進(jìn)行排序:    var Data = [{ id: 7, name: "333", code: "333", type: 3, hasParent: true, parentId: 4 },{ id: 6, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: false, parentId: null },{ id: 5, name: "111", code: "111", type: 3, hasParent: true, parentId: 4 },{ id: 4, name: "22", code: "22", type: 1, hasParent: false, parentId: null },{ id: 3, name: "yyy", code: "yyyy", type: 2, hasParent: false, parentId: null },{ id: 2, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: true, parentId: 1 },{ id: 1, name: "cbcvb", code: "cvbcvcbv", type: 2, hasParent: false, parentId: null }];問(wèn)題出在哪里?我怎么能解決這個(gè)問(wèn)題 ????
查看完整描述

1 回答

?
慕娘9325324

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊

如果它沒(méi)有嵌套元素,我認(rèn)為你可以做一些類似的事情(但不會(huì)給你相同的結(jié)果 - 真的我認(rèn)為你有一個(gè)錯(cuò)誤,parentId的3不是6?


this.sorted=[];

//get all the elements that has parent

const childs=this.data.filter(x=>x.parentId).sort((a,b)=>a.parentId-b.parentId)


//for each

childs.forEach((x,i)=>{

  this.sorted.push(x)     //<--add to sorted

                          //if is the last or the next has diferent parent

  if (i==childs.length-1 || childs[i+1].parentId!=x.parentId)

     this.sorted.push(this.data.find(p=>p.id==x.parentId)) //<--add the parent


})

//finally include the elements that is not in the list

this.data.forEach(x=>{

  if (!this.sorted.find(s=>s.id==x.id))

    this.sorted.push(x)

})


查看完整回答
反對(duì) 回復(fù) 2022-09-29
  • 1 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)