How to add index in an array

1

I'm creating a Javascript Array in the following format:

series = [{
                name: 'Tokyo',
                data: [7.0, 6.9, 9.5]
            }, {
                name: 'New York',
                data: [-0.2, 0.8, 5.7]
            }]

But in some cases, I have to add new values in the index data :

data: [-0.2, 0.8, 5.7, 2.7, 8.9, ...]

Does anyone know how to do this?

    
asked by anonymous 01.08.2014 / 17:54

2 answers

4

You can use the javascript push () method to do what you want.

For example, having your array:

series = [{
    name: 'Tokyo',
    data: [7.0, 6.9, 9.5]
}, {
    name: 'New York',
    data: [-0.2, 0.8, 5.7]
}]

If you want to add an element to each data , you go through the array in .each() and add what you want. Something like:

    $.each(series, function (index, itemData) {
       itemData.data.push(12);
    });

I leave here JSFiddle to see. In this example I am adding 12 to data

    
01.08.2014 / 18:05
4

You can add new values, using .push() :

series[1].data.push(5)
series[1].data
2014-08-01 13:00:41.524[-0.2, 0.8, 5.7, 5]
    
01.08.2014 / 18:02