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

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

二維數(shù)組加對(duì)應(yīng)關(guān)系轉(zhuǎn)換成樹形結(jié)構(gòu)的問題

二維數(shù)組加對(duì)應(yīng)關(guān)系轉(zhuǎn)換成樹形結(jié)構(gòu)的問題

慕田峪4524236 2019-02-11 16:18:22
石墨文檔:https://shimo.im/docs/h2J6PUv...希望通過源數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換成目標(biāo)樹形結(jié)構(gòu),直接從人的角度可以拼出結(jié)果,但如何通過代碼實(shí)現(xiàn)轉(zhuǎn)換?請(qǐng)大家?guī)兔?,謝謝!最終需要實(shí)現(xiàn)的語言 JS,其他語言也可以提供下代碼思路目標(biāo)表格源數(shù)據(jù)結(jié)構(gòu)1. 數(shù)據(jù) (表格原始數(shù)據(jù))[["報(bào)表測試","","","","","1"],["單元格", "","跨行合并","指標(biāo)列","","2"],["statis_date","公司","","太原","福州","4"]]2. 關(guān)系 (表示單元格的合并行和列的情況)[ { col: 0, colspan: 5, row: 0, rowspan: 1 }, { col: 0, colspan: 2, row: 1, rowspan: 1 }, { col: 2, colspan: 1, row: 1, rowspan: 2 }, { col: 3, colspan: 2, row: 1, rowspan: 1 },]3.說明關(guān)系中 col 代表 列序號(hào), row 代表 行序號(hào), colspan 代表 跨列數(shù) ,rowspan 代表 跨行數(shù)合并不能僅依靠空數(shù)據(jù)就合并,需要根據(jù)關(guān)系進(jìn)行定位合并的位置,空可能也會(huì)單獨(dú)單元格顯示存在一條默認(rèn)的關(guān)系即,沒有在關(guān)系中標(biāo)識(shí)的單元格的關(guān)系即為 colspan 1 rowspan 1目標(biāo)數(shù)據(jù)結(jié)構(gòu)目標(biāo)數(shù)據(jù)中 層級(jí) 表示 行,子元素表示下面的列內(nèi)容需要實(shí)現(xiàn)行合并和列合并
查看完整描述

1 回答

?
森欄

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

直接 DFS 遍歷一遍即可。當(dāng)然,你的數(shù)據(jù)能生成樹的前提是每個(gè)項(xiàng)的中間不會(huì)對(duì)應(yīng)上一行某個(gè)項(xiàng)的開始。

function genTree (data, relations) {


  const relationMap = relations.reduce((map, rel) => map.set(`${rel.row},${rel.col}`, rel), new Map())


  const root = []


  _gen(root, data, 0, 0, data[0].length, relationMap)


  return root


  function _gen (children, data, row, colStart, colEnd, relationMap) {

    if (row >= data.length) { return }


    for (let col = colStart; col < colEnd;) {

      const lastSameHeadRel = relationMap.get(`${row - 1},${col}`)

      if (lastSameHeadRel && lastSameHeadRel.row + lastSameHeadRel.rowspan > row) {

        // 跨行占位

        col += lastSameHeadRel.colspan

        break

      }


      let child = {

        label: data[row][col],

        children: []

      }


      children.push(child)


      const relation = relationMap.get(`${row},${col}`)

      const nextRow = row + (relation ? relation.rowspan : 1)

      const nextColStart = col

      const nextColEnd = col + (relation ? relation.colspan : 1)

      _gen(child.children, data, nextRow, nextColStart, nextColEnd, relationMap)


      col = nextColEnd

    }

  }

}

const data = [

  ["報(bào)表測試","","","","","1"],

  ["單元格", "","跨行合并","指標(biāo)列","","2"],

  ["statis_date","公司","","太原","福州","4"]

]


const relations = [

  { col: 0, colspan: 5, row: 0, rowspan: 1 },

  { col: 0, colspan: 2, row: 1, rowspan: 1 },

  { col: 2, colspan: 1, row: 1, rowspan: 2 },

  { col: 3, colspan: 2, row: 1, rowspan: 1 },

]


console.log(JSON.stringify(genTree(data, relations), null, '  '))


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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