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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在類型腳本和角度中對父項和子項進行排序

在類型腳本和角度中對父項和子項進行排序

慕桂英3389331 2022-09-29 16:47:11
我有這個列表: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}我需要按父級和子級對此列表進行排序。如果項目的父值等于另一個項目的 id 的值,則應將具有 parentId 值的項目放在父值等于 id 值的項目下。喜歡這個列表: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}我寫了這段代碼,但它不起作用,沒有對項目列表進行排序:    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 }];問題出在哪里?我怎么能解決這個問題 ????
查看完整描述

1 回答

?
慕娘9325324

TA貢獻1783條經(jīng)驗 獲得超4個贊

如果它沒有嵌套元素,我認為你可以做一些類似的事情(但不會給你相同的結(jié)果 - 真的我認為你有一個錯誤,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)

})


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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