在 JavaScript 中将数据结构转换为树数据
Transform data structure to tree data in JavaScript
我有这样的数据结构:
data = [
{
parent: {name: 'Sheet'},
firstArray: [{sampleVar: 0.3, typeName: 'active'}, {sampleVar: 0.4, typeName: 'active'}],
secondArray: [{sampleVar: 1.2, typeName: 'passive'}, {sampleVar: 1.3 , typeName: 'passive'}]
},...
]
我想把它转换成这样的树数据:
treeData = [
{
parent: {
name: 'Sheet',
children: [{
typeName: 'active',
children: [
{sampleVar: 0.3},
{sampleVar: 0.4}
]
},
{
typeName: 'passive',
children: [
{sampleVar: 1.2},
{sampleVar: 1.3}
]
},...
]
},...
}
];
我不得不提到我的变量 typeName
对每个数组都有相同的值。有谁知道实现此目的的好方法?
考虑到每个数组有 2 个项目,只有每个数组都有自己的类型名称。
var result = [
{
parent: {
name: data[0].parent.name,
children: []
}
}
];
for (const property in data[0]) {
if (property == "parent") continue;
let array = data[0][property];
result[0].parent.children.push({
typeName: array[0].typeName,
children: [
{
sampleVar: array[0].sampleVar
},
{
sampleVar: array[1].sampleVar
}
]
});
}
console.log(result);
您可以使用 Object.values
和 map
来获得结果。我使用了一个变化更大的示例输入:
const data = [{
parent: {name: 'SheetA'},
justOne: [{sampleVar: 0.3, typeName: 'active'}],
hasTwo: [{indicator: 1.2, typeName: 'passive'}, {indicator: 1.3 , typeName: 'passive'}]
}, {
parent: {name: 'SheetB'},
hasThree: [{otherVar: 9.3, typeName: 'selected'}, {otherVar: 9.4, typeName: 'selected'}, {otherVar: 9.5, typeName: 'selected'}],
secondArray: [{message: 7.2, typeName: 'locked'}, {message: 7.3 , typeName: 'locked'}]
}];
const treeData = data.map(({parent, ...rest}) => ({
...parent,
children: Object.values(rest).map(arr => ({
typeName: arr[0]?.typeName,
children: arr.map(({typeName, ...rest}) => rest)
}))
}));
console.log(treeData);
我有这样的数据结构:
data = [
{
parent: {name: 'Sheet'},
firstArray: [{sampleVar: 0.3, typeName: 'active'}, {sampleVar: 0.4, typeName: 'active'}],
secondArray: [{sampleVar: 1.2, typeName: 'passive'}, {sampleVar: 1.3 , typeName: 'passive'}]
},...
]
我想把它转换成这样的树数据:
treeData = [
{
parent: {
name: 'Sheet',
children: [{
typeName: 'active',
children: [
{sampleVar: 0.3},
{sampleVar: 0.4}
]
},
{
typeName: 'passive',
children: [
{sampleVar: 1.2},
{sampleVar: 1.3}
]
},...
]
},...
}
];
我不得不提到我的变量 typeName
对每个数组都有相同的值。有谁知道实现此目的的好方法?
考虑到每个数组有 2 个项目,只有每个数组都有自己的类型名称。
var result = [
{
parent: {
name: data[0].parent.name,
children: []
}
}
];
for (const property in data[0]) {
if (property == "parent") continue;
let array = data[0][property];
result[0].parent.children.push({
typeName: array[0].typeName,
children: [
{
sampleVar: array[0].sampleVar
},
{
sampleVar: array[1].sampleVar
}
]
});
}
console.log(result);
您可以使用 Object.values
和 map
来获得结果。我使用了一个变化更大的示例输入:
const data = [{
parent: {name: 'SheetA'},
justOne: [{sampleVar: 0.3, typeName: 'active'}],
hasTwo: [{indicator: 1.2, typeName: 'passive'}, {indicator: 1.3 , typeName: 'passive'}]
}, {
parent: {name: 'SheetB'},
hasThree: [{otherVar: 9.3, typeName: 'selected'}, {otherVar: 9.4, typeName: 'selected'}, {otherVar: 9.5, typeName: 'selected'}],
secondArray: [{message: 7.2, typeName: 'locked'}, {message: 7.3 , typeName: 'locked'}]
}];
const treeData = data.map(({parent, ...rest}) => ({
...parent,
children: Object.values(rest).map(arr => ({
typeName: arr[0]?.typeName,
children: arr.map(({typeName, ...rest}) => rest)
}))
}));
console.log(treeData);