0

I am trying to create an object which contains an array of objects in 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'}
};

I dont understand what i am doing wrong, please help

2
  • 1
    What's the issue ? Commented Jun 1, 2016 at 9:51
  • 1
    On second read, it looks like you need to add square brackets [ and ]. What you're probably looking is to specify the inner objects into an array. Just wrap the objects in square brackets and specify a key identifier for the array. Commented Jun 1, 2016 at 9:53

4 Answers 4

3

You forgot the array-part:

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'}]
};
Sign up to request clarification or add additional context in comments.

Comments

1

JS objects must have keys.

You could switch to an array of objects:

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'}
];

or give the sub-objects keys: (making an object of objects)

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'}
};

or if you REALLY DO want an object which contains an array of objects:

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'}
    ]
};

Comments

1

If you want to use an array you should have used square brackets:

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'}
];

Comments

0

Array of Objects is something like [{},{},{},...] and Object of array of an objects is something like that {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'}]
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.