Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript update a value in an array within an array

I'm using the Flot Chart plugin to construct a bar chart. When the script receives new data, I need it to increase that value by one.

var data = [["A", 0], ["B", 0], ["C", 0], ["D", 0], ["E", 0]]
$.plot("#placeholder", [data], options);
channel.on("data", function (receiveddata) {
    data[receiveddata] = data[receiveddata] + 1
    $.plot("#placeholder", [data], options);
});

Now say the the channel receives "A" as the data, I want to increase by one and so on. My code shows what i've tried and also

data.recieveddata

but nothing is working.

like image 865
Blease Avatar asked Feb 11 '26 07:02

Blease


1 Answers

You'd do that with :

data[0][1]++;

the array would now be :

[ ["A", 1], ["B", 0], ["C", 0], ["D", 0], ["E", 0] ]
//^     ^
//|     | 1 in second array
//0 in first array

FIDDLE

To use A as a key, you need an object:

var data = {A:0, B:0, C:0};

data[receiveddata] = 2;
like image 137
adeneo Avatar answered Feb 13 '26 19:02

adeneo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!