Leo Messi
Leo Messi

Reputation: 6166

Add a sub-array into an object in Javascript

I have this array of objects:

    myList = [
        { name: 'line', icon:'line-chart' },
        { name : 'spline', icon: 'spline-chart' },
        { name : 'bar', icon: 'bar-chart' }
    ];

Each of these objects represents a button. When that button is clicked it should activate that type of chart.

Another functionality that I must create is that each of these buttons when it is clicked, it activates (or deactivates) other buttons.

For example, line button must activate 3 other buttons while, spline and bar must activate only one each.

line - single, double, triple
spline - single
bar - triple

The set of secondary buttons is this:

    mySecondList = [
        { name: 'Single', icon:'a' },
        { name : 'Double', icon: 'b' },
        { name : 'Triple', icon: 'c' }
    ];

My problem comes here when I want to make a connection between these two.

I tried to incorporate the second array into the first one like this:

myList = [
    { name: 'line', icon:'line-chart', [{ name: 'Single', icon:'a' }, { name : 'Double', icon: 'b' }, { name : 'Triple', icon: 'c' }] },

    { name : 'spline', icon: 'spline-chart', [{ name: 'Single', icon:'a' }] },

    { name : 'bar', icon: 'bar-chart', [{ name: 'Single', icon:'a' }] }
];

but it seems it's not the correct way.

Any suggestions?

Upvotes: 1

Views: 284

Answers (4)

Davide Gozzi
Davide Gozzi

Reputation: 121

The inner element of "myList" list are objects, and as object every attribute need a name so the array that u try to add can't me added cause of the missing name... the correct code should be:

myList = [
    { name: 'line', icon:'line-chart', myArray:[{ name: 'Single', icon:'a' }, { name : 'Double', icon: 'b' }, { name : 'Triple', icon: 'c' }] },

    { name : 'spline', icon: 'spline-chart', myArray:[{ name: 'Single', icon:'a' }] },

    { name : 'bar', icon: 'bar-chart', myArray:[{ name: 'Single', icon:'a' }] } 

];

Upvotes: 1

vpalade
vpalade

Reputation: 1437

Add a name to the array. Use something like this:

var myList = [
    { name: 'line', icon:'line-chart', arr: [{ name: 'Single', icon:'a' }, { name : 'Double', icon: 'b' }, { name : 'Triple', icon: 'c' }] },

    { name : 'spline', icon: 'spline-chart', arr: [{ name: 'Single', icon:'a' }] },

    { name : 'bar', icon: 'bar-chart', arr: [{ name: 'Single', icon:'a' }] }
];

Upvotes: 1

Maher
Maher

Reputation: 2547

Wrong

myList = [
{ name: 'line', icon:'line-chart', [{ name: 'Single', icon:'a' }, { name : 'Double', icon: 'b' }, { name : 'Triple', icon: 'c' }] }
];

you can't insert array in object without put param

Right

myList = [
{ name: 'line', icon:'line-chart', data: [{ name: 'Single', icon:'a' }, { name : 'Double', icon: 'b' }, { name : 'Triple', icon: 'c' }] }
];

There you have data param as array which you can put objects on it

myList[0].data.push({ name : 'Triple', icon: 'c' });

[0] mean first object in myList

Upvotes: 1

Gilad Bar
Gilad Bar

Reputation: 1322

You can't just add a value to an object, you need to comply to the key-value format.

Meaning:

{ name : 'spline', icon: 'spline-chart', FIELD_NAME: [{ name: 'Single', icon:'a' }] }

Upvotes: 2

Related Questions