给出在其中具有父子关系的对象的扁平数组的列表。我需要转换为原始ng树表数据结构。 我想一次扩大所有的孩子。
我的最终目标将是具有以下结构:
str_erreur = ''
if len(list_pt_ref_base) == len(list_pt_ref_ext):
nb_pts = len(list_pt_ref_base)
array_base = np.zeros((nb_pts, 3))
array_ext = np.zeros((nb_pts, 3))
lg_array = 0
for index in range(0, nb_pts):
array_base[lg_array] = list_pt_ref_base[index][:3]
array_ext[lg_array] = list_pt_ref_ext[index][:3]
lg_array += 1
M = superimposition_matrix(array_base, array_ext, scale=True)
# valeurs de sortie
xyzrpw = set_mat4x4_2_xyzrpw(M)
mat_rdk = mat4x4_numpy_2_rdk(M)
if not (not np.all(M) and xyzrpw and mat_rdk):
return False
return RetVal([True, dt.now, 'matrice de passage calculée avec succès !', M, xyzrpw, mat_rdk])
else:
str_erreur = 'Les tailles du tableau du référentiel base('
str_erreur += str(len(list_pt_ref_base))
str_erreur += ') et du référentiel extérieur('
str_erreur += str(len(list_pt_ref_ext))
str_erreur += ') ne sont pas de la même taille !'
print(str_erreur)
RetVal([False, dt.now, str_erreur])
list_ext_brute = [[406.93, -373.8, 2559.99, 0.0, -0.0, 0.0],
[612.81, -65.74, 2566.76, 0.0, -0.0, 0.0],
[679.68, 520.63, 2542.36, 0.0, -0.0, 0.0],
[271.24, 532.19, 2612.08, 0.0, -0.0, 0.0],
[114.43, -31.73, 2439.24, 0.0, -0.0, 0.0],
[-157.93, -220.7, 2490.9, 0.0, -0.0, 0.0],
[-200.13, -350.38, 2501.29, 0.0, -0.0, 0.0],
[-722.9, -260.64, 2556.52, 0.0, -0.0, 0.0],
[-750.43, 160.15, 2551.35, 0.0, -0.0, 0.0],
[-759.14, 488.8, 2545.55, 0.0, -0.0, 0.0],
[-358.39, 151.63, 2416.5, 0.0, -0.0, 0.0],
[-289.58, -60.31, 2410.38, 0.0, -0.0, 0.0],
[-86.04, 153.06, 2369.19, 0.0, -0.0, 0.0],
[-9.05, -92.79, 2361.71, 0.0, -0.0, 0.0]]
list_base_brute = [[2443.9128, -501.7427, -630.8925, -179.0604, 0.4017, 145.2487],
[2126.8356, -703.2691, -678.1219, -179.6825, 0.552, 124.2606],
[1534.3236, -757.3283, -678.0219, 179.9398, 0.6337, 88.9192],
[1532.8918, -339.4951, -682.6528, 179.7809, 0.5975, 74.2169],
[2103.9974, -226.6539, -472.8035, -179.4523, 0.3239, 153.7627],
[2297.246, 47.3245, -475.6743, -179.4523, 0.3237, 153.7627],
[2429.3814, 88.5478, -476.0224, -179.7454, -0.5832, -109.2463],
[2353.675, 614.8282, -447.7145, -179.7454, -0.5833, -109.2462],
[1931.7996, 651.5278, -448.4745, -179.7454, -0.5835, -109.2461],
[1604.6555, 664.6893, -448.8943, -179.7455, -0.5835, -109.2461],
[1925.4354, 246.4732, -379.1885, -179.7455, -0.5836, -109.2458],
[2137.8614, 172.319, -378.1896, -179.7455, -0.5836, -109.2459],
[1919.7934, -31.8006, -376.11, -179.7455, -0.5837, -109.2459],
[2164.5074, -117.227, -374.9585, -179.7455, -0.5837, -109.2459]]
mat_ = set_frame_passage_(list_base_brute, list_ext_brute)
if mat_.b_done:
print('-------------------------------------------------------------------------------------------------------')
print('xyzrpw = ' + str(mat_.res2))
print('-------------------------------------------------------------------------------------------------------')
print('vecteur translation : x=' + str(mat_.res2[0]) + ', y=' + str(mat_.res2[1]) + ', z=' + str(mat_.res2[2]))
print('angles de rotation : r=' + str(mat_.res2[3]) + ', p=' + str(mat_.res2[4]) + ', w=' + str(mat_.res2[5]))
print('----------------------------------------------------------------------------')
print('Matrice de rotation numpy 4x4:')
print(mat_.res1)
print('----------------------------------------------------------------------------')
print('Matrice de rotation roboDK 4x4:')
print(mat_.res3)
else:
print(mat_.str_msg)
给出json数组:
[{
"data":{
"name":"Pictures",
"size":"150kb",
"type":"Folder"
},
"children":[
{
"data":{
"name":"barcelona.jpg",
"size":"90kb",
"type":"Picture"
}
},
{
"data":{
"name":"primeui.png",
"size":"30kb",
"type":"Picture"
}
},
{
"data":{
"name":"optimus.jpg",
"size":"30kb",
"type":"Picture"
}
}
]
}]
]
答案 0 :(得分:1)
具有递归函数的解决方案
function getDataByParentId(data, parent) {
const result = data
.filter(d => d.parentId === parent);
if (!result && !result.length) {
return null;
}
return result.map(({ dataId, name, description }) =>
({ dataId, name, description, children: getDataByParentId(data, dataId) }))
}
const data = [{
dataId: 1,
name: 'test1',
description: 'some desc',
parentId: null
},
{
dataId: 2,
name: 'test2',
description: 'some desc',
parentId: 1
},
{
dataId: 3,
name: 'test3',
description: 'some desc',
parentId: 1
},
{
dataId: 4,
name: 'test4',
description: 'some desc',
parentId: null
},
{
dataId: 5,
name: 'test5',
description: 'some desc',
parentId: 4
},
{
dataId: 6,
name: 'test6',
description: 'some desc',
parentId: 5
}
];
console.log(getDataByParentId(data, null));
答案 1 :(得分:0)
data = [
{
dataId: 1,
name: "test1",
description: 'some desc',
parentId: null
},
{
dataId: 2,
name: "test2",
description: 'some desc',
parentId: 1
},
{
dataId: 3,
name: "test3",
description: 'some desc',
parentId: 4
},
{
dataId: 4,
name: "test4",
description: 'some desc',
parentId: null
}
]
// find leaf who does not have child using recursive
function findLeaf(data, curIndex, parIndex) {
const childId = data[curIndex].dataId;
for(let i = 0; i < data.length; i++) {
if(data[i].parentId == childId) {
return findLeaf(data, i, curIndex);
}
}
return [curIndex, parIndex];
}
// check if array contains child
// and returns child index and parent index
function getChildIndex(data) {
for(let i = 0; i < data.length; i++) {
if(data[i].parentId != null) {
for(let j = 0; j < data.length; j++) {
if(data[j].dataId == data[i].parentId)
return [i, j];
}
}
}
return [-1];
}
function normalize(data) {
// get child and start from it
let childIndex = getChildIndex(data);
while(childIndex[0] !== -1) {
// check if dataId and parentId are same
// if it's same, set parentId to null
// to remove infinite recursive call
if(childIndex[0] == childIndex[1]) {
data[childIndex[0]].parentId = null;
childIndex = getChildIndex(data);
continue;
}
// get leaf index and its parent index
const [cIndex, pIndex] = findLeaf(data, childIndex[0], childIndex[1]);
// correcting data structure before inserting child to parent
if(data[pIndex].children == undefined)
data[pIndex].children = [];
delete data[cIndex].parentId;
delete data[cIndex].dataId;
if(data[cIndex].children != undefined) {
cchildren = data[cIndex].children;
delete data[cIndex].children;
data[cIndex] = { data: data[cIndex], children: cchildren };
}
// insert children to parent
data[pIndex].children.push({ data: data[cIndex] });
// reconfiguring data by removing inserted child
data = [...data.slice(0, cIndex), ...data.slice(cIndex+1)];
// check if there is child left, to loop again
childIndex = getChildIndex(data);
}
// there is no child, it's time to normalize parent structure.
for(let i = 0; i < data.length; i++) {
delete data[i].dataId;
delete data[i].parentId;
//const children = data[i].children;
//delete data[i].children;
//data[i] = children ? { data: data[i], children: children } : { data: data[i] }
}
return data;
}
console.log(normalize(data));
答案 2 :(得分:0)
这是“滥用” filter
的可能解决方案。这个想法是首先建立从dataId
到节点的简单映射,然后使用它在filter
迭代中构建树。
const data = [{
dataId: 1,
name: 'test1',
description: 'some desc',
parentId: null
},
{
dataId: 2,
name: 'test2',
description: 'some desc',
parentId: 1
},
{
dataId: 3,
name: 'test3',
description: 'some desc',
parentId: 1
},
{
dataId: 4,
name: 'test4',
description: 'some desc',
parentId: null
},
{
dataId: 5,
name: 'test5',
description: 'some desc',
parentId: 4
},
{
dataId: 6,
name: 'test6',
description: 'some desc',
parentId: 5
}
];
let dataMap = data.reduce((m, d) => {
m[d.dataId] = Object.assign({}, d);
return m;
}, {});
const trees = data.filter(d => {
if (d.parentId !== null) { // assign child to its parent
let parentNode = dataMap[d.parentId];
if (!('children' in parentNode)) parentNode['children'] = [];
parentNode.children.push(dataMap[d.dataId]);
return false;
}
return true; // root node, do nothing
}).map(d => dataMap[d.dataId]);
console.log(trees);
当然,您可以进行进一步的调整以仅从数据对象中获取某些属性。
答案 3 :(得分:-1)
function getDesigTreeRecursive(data, parent) {
const result = data.filter((d) => d.parent === parent);
if (!result && !result.length) {
return null;
}
return result.map(({ dataId, name, description}) => ({
data: { dataId, name, description},
children: getDesigTreeRecursive(data, dataId),
}));
}
console.log({ data: tree_data });