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

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

點(diǎn)分隔字符串?dāng)?shù)組中的樹(shù)

點(diǎn)分隔字符串?dāng)?shù)組中的樹(shù)

慕后森 2022-10-13 16:03:47
我有一個(gè)點(diǎn)分隔的字符串?dāng)?shù)組,如下所示data = [    'Europe.UK.London.TrafalgarSq',     'Europe.UK.London.HydePark',     'Europe.UK.London.OxfordStreet',     'Europe.UK.London.City.Bank',     'Europe.France.Paris',     'Europe.France.Bordeaux'},]我想構(gòu)建以下嵌套對(duì)象樹(shù)。萬(wàn)一這很重要,這適用于將要使用Tree Layers Controlleaflet的地圖var tree = {    label: 'Places',    selectAllCheckbox: 'Un/select all',    children: [        {            label: 'Europe',            selectAllCheckbox: true,            children: [                {                    label: 'Europe.UK',                    selectAllCheckbox: true,                    children: [                        {                            label: 'Europe.UK.London',                            selectAllCheckbox: true,                            children: [                                {label: 'Europe.UK.London.TrafalgarSq'},                                {label: 'Europe.UK.London.HydePark'},                                {label: 'Europe.UK.London.OxfordStreet'},                                {                                    label: 'Europe.UK.London.City',                                    selectAllCheckbox: true,                                    children: [                                        {label: 'Europe.UK.London.City.Bank'},                                    ]                                },                            ]                        },                        {                            label: 'Europe.France',                            selectAllCheckbox: true,                            children: [                                {label: 'Europe.France.Paris'},                                {label: 'Europe.France.Bordeaux'},                            ]                        },                    ]                }            ]        }    ]};請(qǐng)問(wèn)這棵樹(shù)怎么做?
查看完整描述

2 回答

?
夢(mèng)里花落0921

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

您可以使用mapper具有部分路徑(或label)的對(duì)象作為鍵,并使用對(duì)樹(shù)中對(duì)象的引用作為其值。split路徑.和reduce數(shù)組tree作為initialValue。如果path尚不存在,請(qǐng)將其添加到映射器和樹(shù)中。在每次迭代中返回嵌套對(duì)象。


const data = ["Europe.UK.London.TrafalgarSq","Europe.UK.London.HydePark","Europe.UK.London.OxfordStreet","Europe.UK.London.City.Bank","Europe.France.Paris","Europe.France.Bordeaux"],

    mapper = {},

    tree = {

      label: 'Places',

      selectAllCheckbox: 'Un/select all',

      children: []

    }


for (const str of data) {

  let splits = str.split('.'),

      label = '';


  splits.reduce((parent, place) => {

    if (label)

      label += `.${place}`

    else

      label = place


    if (!mapper[label]) {

      const o = { label };

      mapper[label] = o;

      parent.selectAllCheckbox = true

      parent.children = parent.children || [];

      parent.children.push(o)

    }


    return mapper[label];

  }, tree)

}


console.log(tree)


查看完整回答
反對(duì) 回復(fù) 2022-10-13
?
明月笑刀無(wú)情

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

您可以使用減少嵌套對(duì)象的迭代方法。


var data = ['Europe.UK.London.TrafalgarSq', 'Europe.UK.London.HydePark', 'Europe.UK.London.OxfordStreet', 'Europe.UK.London.City.Bank', 'Europe.France.Paris', 'Europe.France.Bordeaux'],

    children = data.reduce((r, s) => {

        s.split('.').reduce((q, _, i, a) => {

            q.selectAllCheckbox = true;

            var label = a.slice(0, i + 1).join('.'),

                temp = (q.children = q.children || []).find(o => o.label === label);

            if (!temp) q.children.push(temp = { label });

            return temp;

        }, r);

        return r;

    }, { children: [] }).children,

    tree = { label: 'Places', selectAllCheckbox: 'Un/select all', children };


console.log(tree);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對(duì) 回復(fù) 2022-10-13
  • 2 回答
  • 0 關(guān)注
  • 126 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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