1

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?

2
  • you can't do this without param Commented Feb 7, 2018 at 10:50
  • 1
    You have to make a object key to assign an array to it. Commented Feb 7, 2018 at 10:54

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

1

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

];

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.