希望通過源數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換成目標(biāo)樹形結(jié)構(gòu),直接從人的角度可以拼出結(jié)果,但如何通過代碼實(shí)現(xiàn)轉(zhuǎn)換?請大家?guī)兔?,謝謝!
最終需要實(shí)現(xiàn)的語言 JS,其他語言也可以提供下代碼思路
[
["報表測試","","","","","1"],
["單元格", "","跨行合并","指標(biāo)列","","2"],
["statis_date","公司","","太原","福州","4"]
]
[
{ 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 },
]
col
代表 列序號
, row
代表 行序號
, colspan
代表 跨列數(shù)
,rowspan
代表 跨行數(shù)
colspan 1 rowspan 1
目標(biāo)數(shù)據(jù)中
層級 表示 行
,子元素表示下面的列內(nèi)容
需要實(shí)現(xiàn)行合并和列合并
[
{
"label": "報表測試",
"children": [
{
"label": "單元格",
"children": [
{
"label": "statis_date",
"children": []
},
{
"label": "公司",
"children": []
}
]
},
{
"label": "跨行合并",
"children": []
},
{
"label": "指標(biāo)列",
"children": [
{
"label": "太原",
"children": []
},
{
"label": "福州",
"children": []
}
]
}
]
},
{
"label": "1",
"children": [
{
"label": "2",
"children": [
{
"label": "4",
"children": []
}
]
}
]
}
]
// 表數(shù)據(jù)
const header = [
["報表測試","","","","","1"],
["單元格", "","跨行合并","指標(biāo)列","","2"],
["statis_date","公司","","太原","福州","4"]
]
// 關(guān)系
const relation = [
{ 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 },
]
// 已知非默認(rèn)關(guān)系 (即 colspan rowspan 有任何一個不為1)
const relObj = {}
for (let i = 0; i < rel.length; i += 1) {
const item = rel[i]
relObj[`${item.col}_`${item.row}`] = item
}
// 列數(shù) (有個前提,每行列數(shù)一致)
const colLength = header[0].length
// 行數(shù)
const rowLength = header.length
// 整個表每個單元格的關(guān)系,并保存單元格數(shù)據(jù)
const relArr= []
for (let i = 0; i < rowLength; i += 1) {
const row = i
for (let j = 0; j < colLength; j += 1) {
const col = j
if (relObj[col + '_' + row]) {
const obj = relObj[col + '_' + row]
relArr.push({
label: header[row][col],
col: col,
row: row,
colspan: obj.colspan,
rowspan: obj.rowspan
})
} else {
relArr.push({
label: header[row][col],
col: col,
row: row,
colspan: 1,
rowspan: 1,
})
}
}
}
!TODO
Node.java
public class Node {
String label;
List<Node> children = new ArrayList<Node>();
int col;
int colspan;
int row;
int rowspan;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public List<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
@Override
public String toString() {
return "Node [label=" + label + ", children=" + children + "]";
}
public static void main(String[] args) {
String oldData = "[[\"報表測試\",\"\",\"\",\"\",\"\",\"1\"],[\"單元格\", \"\",\"跨行合并\",\"指標(biāo)列\(zhòng)",\"\",\"2\"],[\"statis_date\",\"公司\",\"\",\"太原\",\"福州\",\"4\"]]";
String struct = "[{col:0,colspan:5,row:0,rowspan:1},{col:5,colspan:1,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},{col:5,colspan:1,row:1,rowspan:1},{col:0,colspan:1,row:2,rowspan:1},{col:1,colspan:1,row:2,rowspan:1}, {col:3,colspan:1,row:2,rowspan:1}, {col:4,colspan:1,row:2,rowspan:1}, {col:5,colspan:1,row:2,rowspan:1} ]";
List<Struct> structs = JSONArray.parseArray(struct, Struct.class);
List<List<Node>> nodeArray = new ArrayList<List<Node>>();
for (int i = 0; i < structs.size(); i++) {
nodeArray.add(new ArrayList<Node>());
}
System.err.println(structs);
JSONArray jsonArray = (JSONArray) JSONArray.parse(oldData);
List<Node> root = new ArrayList<Node>();
int row = 0;
for (Struct structItem : structs) {
Node node = new Node();
JSONArray jsonArray2 = (com.alibaba.fastjson.JSONArray) jsonArray.get(structItem.getRow());
String label = jsonArray2.getString(structItem.getCol());
node.setLabel(label);
node.col = structItem.getCol();
node.colspan = structItem.getColspan();
node.row = structItem.getRow();
node.rowspan = structItem.getRowspan();
row = structItem.row;
if (row == 0) {
// 當(dāng)前為第一行
root.add(node);
row = structItem.getRow();
} else {
for (Node node2 : nodeArray.get(structItem.row - 1)) {
if (node2.col <= structItem.getCol() && structItem.getCol() < node2.col + node2.colspan) {
node2.getChildren().add(node);
break;
}
}
}
nodeArray.get(row).add(node);
}
System.err.println(JSONArray.toJSON(root));
}
}
Struct.java
public class Struct {
int col;
int colspan;
int row;
int rowspan;
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public int getColspan() {
return colspan;
}
public void setColspan(int colspan) {
this.colspan = colspan;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getRowspan() {
return rowspan;
}
public void setRowspan(int rowspan) {
this.rowspan = rowspan;
}
@Override
public String toString() {
return "Struct [col=" + col + ", colspan=" + colspan + ", row=" + row + ", rowspan=" + rowspan + "]";
}
}
謝邀,直接 DFS 遍歷一遍即可。當(dāng)然,你的數(shù)據(jù)能生成樹的前提是每個項(xiàng)的中間不會對應(yīng)上一行某個項(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 = [
["報表測試","","","","","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, ' '))
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。