亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

有沒有辦法在 JavaScript/TypeScript 中對數組中的每個數組遞歸執行 .map ?

有沒有辦法在 JavaScript/TypeScript 中對數組中的每個數組遞歸執行 .map ?

寶慕林4294392 2023-07-20 17:02:02
我目前有這個遞歸重復的數組: [    {      "Name": "Test",      "Children": [        {          "Name": "Id",          "Property": "Placeholder",          "Children": [           {             "Name": "Child Id",             "Property": "Placeholder",             "Children": null           }          ]        }    }]為了獲得我想要的結構,我目前有這個:const fixed = data.map(item => item = {        data: {            name: item.Name,        },        children: item.Children.map(child => child = {            name: child.Name,        })    }  )有沒有辦法為每個子數組遞歸地重復我的初始 array.map ?
查看完整描述

3 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

一種可能的解決方案,基于我相信您想要的輸出:


const array = [{

  "Name": "Test",

  "Children": [{

    "Name": "Id",

    "Property": "Placeholder",

    "Children": [{

      "Name": "Child Id 1",

      "Property": "Placeholder",

      "Children": null

    }, {

      "Name": "Child Id 2",

      "Property": "Placeholder",

      "Children": [{

        "Name": "Child Id 3",

        "Property": "Placeholder",

        "Children": null

      }]

    }]

  }]

}];



const map = (arr) => {

  return arr ? arr.map(fix) : null;

}


const fix = (item) => {

  return {

    data: {

      name: item.Name

    },

    children: map(item.Children),

  };

}


console.log(map(array))


或者按照下面的建議,使用簡寫:


const map = arr => arr ? arr.map(fix) : null;


const fix = item => ({

  data: {

    name: item.Name

  },

  children: map(item.Children),

});


查看完整回答
反對 回復 2023-07-20
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

我們可以使用一個基本情況,檢查Children屬性是否為null,如果是,則遞歸停止并返回Name:


const data = [{"Name": "Test", "Children": [{"Name": "Id","Property": "Placeholder","Children": [{"Name": "Child Id","Property": "Placeholder","Children": null }] }] }];


const flattenData = (data) => {

    return data.map(d => {

    //Base case, recursion stops here

    if(!d.Children){

      return d.Name;

    }

    //Continue recursion

    return {data: {name: d.Name},  children: flattenData(d.Children) }

   });

}

console.log(flattenData(data));


查看完整回答
反對 回復 2023-07-20
?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

您可以創建一個轉換子項的函數。如果孩子還有其他孩子,那么該函數將調用自身來遞歸地轉換它們。


const input = [

    {

        "Name": "Test",

        "Children": [

            {

                "Name": "Id",

                "Property": "Placeholder",

                "Children": [

                    {

                        "Name": "Child Id",

                        "Property": "Placeholder",

                        "Children": null

                    }

                ]

            }

        ]

    }

];


const transformChildren = children => children.map(child => ({

    name: child.Name,

    children: child.Children ? transformChildren(child.Children) : null

}));


const output = input.map(item => ({

    data: {

        name: item.Name,

    },

    children: item.Children ? transformChildren(item.Children) : null

}))


console.log(output)


查看完整回答
反對 回復 2023-07-20
  • 3 回答
  • 0 關注
  • 241 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號