这是我的数据
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"}
我需要从Name:创建一个列表,像这样。
servernames = ["Server1","Server2","Server3","Server4","Server5","Server6","Server7","Server8"]
答案 0 :(得分:3)
正如其他人所指出的,您有一个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);
答案 1 :(得分:0)
尝试这个:
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"}
}
const result = Object.keys(tabledata).map(el => tabledata[el].Name)
console.log(result)
答案 2 :(得分:0)
您的JSON:
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"}
};
您可以使用Object.values
获取表数据对象的值数组,然后应用Array.prototype.map
将值转换为服务器名称,如下所示:
const serverNames = Object.values(tabledata).map((server) => server.Name);
console.log(serverNames);
它将打印:
["Server1", "Server2", "Server3", "Server4", "Server5", "Server6", "Server7", "Server8", "Server9"]