gpt4 book ai didi

javascript - 从 JavaScript 对象生成列表

转载 作者:行者123 更新时间:2023-11-28 14:14:19 24 4
gpt4 key购买 nike

这是我的数据:

var tabledata = {
0: {Name: "Server1", PRPName: "NO_PRP", OperatingSystem: "Linux", SLA: "SILVER", Environment: "Accept"}
1: {Name: "Server2", PRPName: "services", OperatingSystem: "Linux", SLA: "SILVER", Environment:"Accept"}
2: {Name: "Server3", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"}
3: {Name: "Server4", PRPName: "services", OperatingSystem: "Linux", SLA: "GOLD", Environment: "Accept"}
4: {Name: "Server5", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"}
5: {Name: "Server6", PRPName: "sql", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"}
6: {Name: "Server7", PRPName: "services", OperatingSystem: "Linux", SLA: "GOLD", Environment: "Production"}
7: {Name: "Server8", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Production"}
8: {Name: "Server9", PRPName: "sql", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Production"}

我需要从名称创建一个列表:像这样。

servernames = ["Server1","Server2","Server3","Server4","Server5","Server6","Server7","Server8"]

最佳答案

正如其他人所指出的,您有一个 JavaScript 对象(数字和表示服务器数据的对象之间的映射),而不是 JSON。

要从此对象获取服务器名称,您可以使用 Object.values首先使用服务器信息提取对象的属性值,然后 map仅获取服务器名称,使用这些名称生成一个数组:

var tabledata = {
0: {Name: "Server1", PRPName: "NO_PRP", OperatingSystem: "Linux", SLA: "SILVER", Environment: "Accept"},
1: {Name: "Server2", PRPName: "services", OperatingSystem: "Linux", SLA: "SILVER", Environment:"Accept"},
2: {Name: "Server3", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"},
3: {Name: "Server4", PRPName: "services", OperatingSystem: "Linux", SLA: "GOLD", Environment: "Accept"},
4: {Name: "Server5", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"},
5: {Name: "Server6", PRPName: "sql", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Accept"},
6: {Name: "Server7", PRPName: "services", OperatingSystem: "Linux", SLA: "GOLD", Environment: "Production"},
7: {Name: "Server8", PRPName: "NO_PRP", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Production"},
8: {Name: "Server9", PRPName: "sql", OperatingSystem: "Windows", SLA: "GOLD", Environment: "Production"}
};
var servernames = Object.values(tabledata).map((server) => server.Name);

console.log(servernames);

关于javascript - 从 JavaScript 对象生成列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58216880/

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