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

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

從具有平坦路徑的對象數(shù)組生成嵌套對象

從具有平坦路徑的對象數(shù)組生成嵌套對象

慕桂英546537 2023-07-06 16:33:48
我正在嘗試將一個對象數(shù)組(數(shù)組中的每個對象都有一個路徑和 id)轉(zhuǎn)換為表示該路徑的樹。例如,對于給定的輸入,輸出如下 [{path: 'foo/bar/baz', id: 1}][    {      "path": "foo",      "children": [        {          "path": "bar",          "children": [            {              "path": "baz",              "children": [],            }          ]        }      ]    }]到目前為止,我有以下代碼:const pathObjs = [    { id: 1, path: 'foo/bar/baz' },  ];const result = [];const level = { result };for (p of pathObjs) {  p.path.split('/').reduce((r, path) => {    if (!r[path]) {      r[path] = { result: [] };      const p = { path, children: r[path].result };      r.result.push(p);    }    return r[path];  }, level);}我無法弄清楚如何id在正確的級別分配每個路徑的 ,以便最終結(jié)果如下所示:[    {      "path": "foo",      "children": [        {          "path": "bar",          "children": [            {              "path": "baz",              "children": [],              // how to add this guy here!              "id": 1,            }          ]        }      ]    }]有人可以將我推向正確的方向嗎?
查看完整描述

2 回答

?
MM們

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

不要在回調(diào)中創(chuàng)建const p局部變量reduce,而是使用完成后可訪問的變量reduce,以便您可以訪問最后分配的對象以添加附加屬性(或多個屬性)。


所以改變:


      const p = { path, children: r[path].result };

      r.result.push(p);

到:


      last = { path, children: r[path].result };

      r.result.push(last);

并last在塊的作用域中定義為局部變量for。完成后reduce,您可以使用Object.assign來改變“葉子”對象,添加額外的屬性:


  Object.assign(last, rest); // rest is whatever needs to be added.

所以你的代碼可以修改如下:


const result = [];

const level = { result };


for (p of pathObjs) {

  let last; // This will be the latest added object

  let { path, ...rest } = p; // Extract the path and the other properties

  path.split('/').reduce((r, path, i, {length}) => {

    if (!r[path]) {

      r[path] = { result: [] };


      last = { path, children: r[path].result };


      r.result.push(last);

    }


    return r[path];

  }, level);

  Object.assign(last, rest); // Extend with the extra properties.

}


查看完整回答
反對 回復(fù) 2023-07-06
?
長風秋雁

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

該解決方案重組了您的原始代碼。主要區(qū)別在于路徑列表是相反的,因此我們從葉節(jié)點而不是根開始。


有一個顯式檢查來查看我們是否位于第一個索引(我們可以假設(shè)它是葉子)?;蛘撸绻敢?,您可以檢查是否result已定義,并依賴它而不是索引。


const pathObjects = [{ id: 1, path: "foo/bar/baz" }];


const result = [];


for (let { id, path } of pathObjects) {

  const obj = path

    .split("/")

    .reverse()

    .reduce((result, path, index) => {

      const node = {

        path,

        children: result ? [result] : []

      };


      if (index === 0) {

        node.id = id;

      }


      return node;

    }, null);


  result.push(obj);

}


console.log(result);


查看完整回答
反對 回復(fù) 2023-07-06
  • 2 回答
  • 0 關(guān)注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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