2 回答
TA贡献1876条经验 获得超5个赞
const
data = [ { target: 'a', source: 'a' }
, { target: 'a', source: 'b' }
, { target: 'b', source: 'c' }
, { target: 'c', source: 'd' }
];
let
CurKey = '',
CurrentChild = null,
result = null;
data.forEach( elm => {
if (elm.target === elm.source)
{
CurKey = elm.target;
result = { "target": CurKey, "children": [] };
CurrentChild = result.children;
}
else if (elm.target === CurKey)
{
CurKey = elm.source;
if (data.findIndex(ef=>ef.target===elm.source) >0 )
{
let newElm = { "target": CurKey, "children": [] };
CurrentChild.push(newElm)
CurrentChild = newElm.children;
}
else
{
let newElm = { "target": CurKey };
CurrentChild.push(newElm)
CurrentChild = null;
CurKey = null;
}
}
});
console.log(JSON.stringify(result));
Second Case, without { target: 'a', source: 'a' } :
const
data = [ { target: 'a', source: 'b' }
, { target: 'b', source: 'c' }
, { target: 'c', source: 'd' }
];
let
CurKey = '',
CurrentChild = null,
result = null;
data.forEach( elm => {
if (CurKey === '')
{
CurKey = elm.target;
result = { "target": CurKey, "children": [] };
CurrentChild = result.children;
}
if (elm.target === CurKey)
{
CurKey = elm.source;
if (data.findIndex(ef=>ef.target===elm.source) >0 )
{
let newElm = { "target": CurKey, "children": [] };
CurrentChild.push(newElm)
CurrentChild = newElm.children;
}
else
{
let newElm = { "target": CurKey };
CurrentChild.push(newElm)
CurrentChild = null;
CurKey = null;
}
}
});
console.log(JSON.stringify(result));
Second Case, without { target: 'a', source: 'a' } :
const
data = [ { target: 'a', source: 'b' }
, { target: 'b', source: 'c' }
, { target: 'c', source: 'd' }
];
let
CurKey = '',
CurrentChild = null,
result = null;
data.forEach( elm => {
if (CurKey === '')
{
CurKey = elm.target;
result = { "target": CurKey, "children": [] };
CurrentChild = result.children;
}
if (elm.target === CurKey)
{
CurKey = elm.source;
if (data.findIndex(ef=>ef.target===elm.source) >0 )
{
let newElm = { "target": CurKey, "children": [] };
CurrentChild.push(newElm)
CurrentChild = newElm.children;
}
else
{
let newElm = { "target": CurKey };
CurrentChild.push(newElm)
CurrentChild = null;
CurKey = null;
}
}
});
console.log(JSON.stringify(result));
Second Case, without { target: 'a', source: 'a' } :
const
data = [ { target: 'a', source: 'b' }
, { target: 'b', source: 'c' }
, { target: 'c', source: 'd' }
];
let
CurKey = '',
CurrentChild = null,
result = null;
data.forEach( elm => {
if (CurKey === '')
{
CurKey = elm.target;
result = { "target": CurKey, "children": [] };
CurrentChild = result.children;
}
if (elm.target === CurKey)
{
CurKey = elm.source;
if (data.findIndex(ef=>ef.target===elm.source) >0 )
{
let newElm = { "target": CurKey, "children": [] };
CurrentChild.push(newElm)
CurrentChild = newElm.children;
}
else
{
let newElm = { "target": CurKey };
CurrentChild.push(newElm)
CurrentChild = null;
CurKey = null;
}
}
});
console.log(JSON.stringify(result));
TA贡献1829条经验 获得超4个赞
var arr_1 = [];
var arr_2 = [];
var arr_3 = [];
var json = {};
var json_2 = {};
var json_3 = {};
var json_4 = {};
arr_3.push(json_4);
json_3.target = 'c';
json_3.children = arr_3;
arr_2.push(json_3);
json_2.target = 'b';
json_2.children = arr_2;
arr_1.push(json_2);
json.target = 'a';
json.children = arr_1;
console.log(json);
动态添加到JSON json_2['target'] = 'c';
添加回答
举报