我正在尝试创建一个包含javascript中对象数组的对象
var alertArray = {
{threshold: 'critical', deviceName: 'Device Agg-02-01', text: 'CPU exceeding policy threshold of 80%', time: '7:00 PM'},
{threshold: 'critical', deviceName: 'Device Leaf-12-22', text: 'Memory utilization exceeding 40%', time: '6:34 PM'},
{threshold: 'warning', deviceName: 'Leaf10-12', text: 'New Software available for upgrade on device', time: '5:10 PM'},
{threshold: 'warning', deviceName: 'Leaf10-11', text: 'New Software available for upgrade on device', time: '4:32 PM'}
};
我不明白我做错了什么,请帮忙
答案 0 :(得分:3)
你忘记了数组部分:
var alertArray = {
someArray: [{threshold: 'critical', deviceName: 'Device Agg-02-01', text: 'CPU exceeding policy threshold of 80%', time: '7:00 PM'},
{threshold: 'critical', deviceName: 'Device Leaf-12-22', text: 'Memory utilization exceeding 40%', time: '6:34 PM'},
{threshold: 'warning', deviceName: 'Leaf10-12', text: 'New Software available for upgrade on device', time: '5:10 PM'},
{threshold: 'warning', deviceName: 'Leaf10-11', text: 'New Software available for upgrade on device', time: '4:32 PM'}]
};
答案 1 :(得分:1)
JS对象必须有密钥。
您可以切换到对象数组:
var alertArray = [
{threshold: 'critical', deviceName: 'Device Agg-02-01', text: 'CPU exceeding policy threshold of 80%', time: '7:00 PM'},
{threshold: 'critical', deviceName: 'Device Leaf-12-22', text: 'Memory utilization exceeding 40%', time: '6:34 PM'},
{threshold: 'warning', deviceName: 'Leaf10-12', text: 'New Software available for upgrade on device', time: '5:10 PM'},
{threshold: 'warning', deviceName: 'Leaf10-11', text: 'New Software available for upgrade on device', time: '4:32 PM'}
];
或给出子对象键:(制作对象的对象)
var alertArray = {
o1:{threshold: 'critical', deviceName: 'Device Agg-02-01', text: 'CPU exceeding policy threshold of 80%', time: '7:00 PM'},
o2:{threshold: 'critical', deviceName: 'Device Leaf-12-22', text: 'Memory utilization exceeding 40%', time: '6:34 PM'},
o3:{threshold: 'warning', deviceName: 'Leaf10-12', text: 'New Software available for upgrade on device', time: '5:10 PM'},
o4:{threshold: 'warning', deviceName: 'Leaf10-11', text: 'New Software available for upgrade on device', time: '4:32 PM'}
};
或者如果您真的想要一个包含对象数组的对象:
var alertArray = {
myObjects: [
{threshold: 'critical', deviceName: 'Device Agg-02-01', text: 'CPU exceeding policy threshold of 80%', time: '7:00 PM'},
{threshold: 'critical', deviceName: 'Device Leaf-12-22', text: 'Memory utilization exceeding 40%', time: '6:34 PM'},
{threshold: 'warning', deviceName: 'Leaf10-12', text: 'New Software available for upgrade on device', time: '5:10 PM'},
{threshold: 'warning', deviceName: 'Leaf10-11', text: 'New Software available for upgrade on device', time: '4:32 PM'}
]
};
答案 2 :(得分:1)
如果你想使用数组,你应该使用方括号:
var alertArray = [
{threshold: 'critical', deviceName: 'Device Agg-02-01', text: 'CPU exceeding policy threshold of 80%', time: '7:00 PM'},
{threshold: 'critical', deviceName: 'Device Leaf-12-22', text: 'Memory utilization exceeding 40%', time: '6:34 PM'},
{threshold: 'warning', deviceName: 'Leaf10-12', text: 'New Software available for upgrade on device', time: '5:10 PM'},
{threshold: 'warning', deviceName: 'Leaf10-11', text: 'New Software available for upgrade on device', time: '4:32 PM'}
];
答案 3 :(得分:0)
对象数组类似于[{},{},{},...]
和对象数组的对象类似于{data:[{},{},{},...]}
var alertArray = {
data: [{threshold: 'critical', deviceName: 'Device Agg-02-01', text: 'CPU exceeding policy threshold of 80%', time: '7:00 PM'},
{threshold: 'critical', deviceName: 'Device Leaf-12-22', text: 'Memory utilization exceeding 40%', time: '6:34 PM'},
{threshold: 'warning', deviceName: 'Leaf10-12', text: 'New Software available for upgrade on device', time: '5:10 PM'},
{threshold: 'warning', deviceName: 'Leaf10-11', text: 'New Software available for upgrade on device', time: '4:32 PM'}]
};