Plant->Route->Turn" 我有一个设备列表,在这个字符串的每个字段上都有一个值和附加数据,就像这样 [ { -6ren">
gpt4 book ai didi

javascript - 为 Angular 动态数据生成嵌套结构

转载 作者:行者123 更新时间:2023-11-30 20:03:24 25 4
gpt4 key购买 nike

我在尝试生成动态结构时遇到问题。我有以下字符串。

"Client->Plant->Route->Turn"

我有一个设备列表,在这个字符串的每个字段上都有一个值和附加数据,就像这样

[
{
"Client": "Client 1",
"Plant": "Plant 1",
"Route": "Route 1",
"Turn": "Turn 1",
"ascends": 10,
"descends": 5,
"deviceId": 1
},
{
"Client": "Client 1",
"Plant": "Plant 1",
"Route": "Route 2",
"Turn": "Turn 1",
"ascends": 10,
"descends": 5,
"deviceId": 2
},
{
"Client": "Client 1",
"Plant": "Plant 1",
"Route": "Route 3",
"Turn": "Turn 1",
"ascends": 10,
"descends": 5,
"deviceId": 3
},
{
"Client": "Client 1",
"Plant": "Plant 2",
"Route": "Route 1",
"Turn": "Turn 1",
"ascends": 10,
"descends": 5,
"deviceId": 4
},
{
"Client": "Client 1",
"Plant": "Plant 1",
"Route": "Route 3",
"Turn": "Turn 4",
"ascends": 10,
"descends": 5,
"deviceId": 5
},
{
"Client": "Client 2",
"Plant": "Plant 1",
"Route": "Route 1",
"Turn": "Turn 1",
"ascends": 10,
"descends": 5,
"deviceId": 6
}
]

我需要在动态 Bootstrap 中显示此信息

Client 1            
Plant 1
Route 1
Turn 1
Route 2
Turn 1
Route 3
Turn 1
Turn 4
Plant 2
Route 1
Turn 1
Client 2
Plant 1
Route 1
Turn 1

我不需要在折叠组件中使用标识,只是为了让它更容易理解。

我使用的是 angular 6,所以我在搜索组件时发现了一个可以生成“n”个嵌套列表的组件。 https://gist.github.com/arniebradfo/5cf89c362cc216df6fc1d9ca4d536b72

下面是它应该是什么样子的例子

[
{
"title": "Client 1",
"children": [
{
"title": "Plant 1",
"children": [
{
"title": "Route 1",
"children": [
{
"title": "Turn 1",
"ascends": 10,
"descends": 5,
"deviceId": 1
}
]
},
{
"title": "Route 2",
"children": [
{
"title": "Turn 1",
"ascends": 10,
"descends": 5,
"deviceId": 2
}
]
},
{
"title": "Route 3",
"children": [
{
"title": "Turn 1",
"ascends": 10,
"descends": 5,
"deviceId": 3
},
{
"title": "Turn 4",
"ascends": 10,
"descends": 5,
"deviceId": 5
}
]
}
]
},
{
"title": "Plant 2",
"children": [
{
"title": "Route 1",
"children": [
{
"title": "Turn 1",
"ascends": 10,
"descends": 5,
"deviceId": 4
}
]
}
]
}
]
},
{
"title": "Client 2",
"children": [
{
"title": "Plant 1",
"children": [
{
"title": "Route 1",
"children": [
{
"title": "Turn 1",
"ascends": 10,
"descends": 5,
"deviceId": 6
}
]
}
]
}
]
}
]

我放置的第一个字符串可以更改,因此可以包含更多项并具有不同的顺序,这就是我需要使其动态化的原因。

我想创建一个类似于组件示例的数组来显示我的数据。

同样在最后一级,它必须显示“附加数据”。

谢谢。

最佳答案

您可以采用一个数组作为键来嵌套项目,并通过减少键和创建需要的对象来减少给定的数据。最后将数据推送到最嵌套的数组。

此方法使用对象的剩余属性。

var data = [{ Client: "Client 1", Plant: "Plant 1", Route: "Route 1", Turn: "Turn 1", ascends: 10, descends: 5, deviceId: 1 }, { Client: "Client 1", Plant: "Plant 1", Route: "Route 2", Turn: "Turn 1", ascends: 10, descends: 5, deviceId: 2 }, { Client: "Client 1", Plant: "Plant 1", Route: "Route 3", Turn: "Turn 1", ascends: 10, descends: 5, deviceId: 3 }, { Client: "Client 1", Plant: "Plant 2", Route: "Route 1", Turn: "Turn 1", ascends: 10, descends: 5, deviceId: 4 }, { Client: "Client 1", Plant: "Plant 1", Route: "Route 3", Turn: "Turn 4", ascends: 10, descends: 5, deviceId: 5 }, { Client: "Client 2", Plant: "Plant 1", Route: "Route 1", Turn: "Turn 1", ascends: 10, descends: 5, deviceId: 6 }],
keys = ['Client', 'Plant', 'Route'],
tree = data.reduce((r, { Turn, ascends, descends, deviceId, ...o }) => {
keys
.reduce((s, k) => {
var temp = s.find(({ title }) => title === o[k]);
if (!temp) {
s.push(temp = { title: o[k], children: [] });
}
return temp.children;
}, r)
.push({ title: Turn, ascends, descends, deviceId });
return r;
}, []);

console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

一种不同的方法,通过使用键数组进行嵌套来动态获取所有属性。

var data = [{ Client: "Client 1", Plant: "Plant 1", Route: "Route 1", Turn: "Turn 1", ascends: 10, descends: 5, deviceId: 1 }, { Client: "Client 1", Plant: "Plant 1", Route: "Route 2", Turn: "Turn 1", ascends: 10, descends: 5, deviceId: 2 }, { Client: "Client 1", Plant: "Plant 1", Route: "Route 3", Turn: "Turn 1", ascends: 10, descends: 5, deviceId: 3 }, { Client: "Client 1", Plant: "Plant 2", Route: "Route 1", Turn: "Turn 1", ascends: 10, descends: 5, deviceId: 4 }, { Client: "Client 1", Plant: "Plant 1", Route: "Route 3", Turn: "Turn 4", ascends: 10, descends: 5, deviceId: 5 }, { Client: "Client 2", Plant: "Plant 1", Route: "Route 1", Turn: "Turn 1", ascends: 10, descends: 5, deviceId: 6 }],
keys = ['Client', 'Plant', 'Route', 'Turn'],
tree = [];


data.reduce((r, o) => {
var p = keys.reduce((s, k) => {
s.children = s.children || [];
var temp = s.children.find(({ title }) => title === o[k]);
if (!temp) {
s.children.push(temp = { title: o[k] });
}
return temp;
}, r);
Object
.keys(o)
.filter(k => !keys.includes(k))
.forEach(k => p[k] = o[k]);
return r;
}, { children: tree });

console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 为 Angular 动态数据生成嵌套结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53145121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com