I am working on yii2
. In my javascript, I have a formula through which I am getting some data. The data is then passed to chart and chart renders it.
var arry_kwh = []; arry_kwh = <?php echo json_encode($dataPointskWh, JSON_NUMERIC_CHECK); ?>; var arry_kwh_diff = []; var i = 0; for (; i < arry_kwh.length - 1; i++) { arry_kwh_diff[i] = { label: arry_kwh[i].label + ((arry_kwh[i + 1].label - arry_kwh[i].label) / 2), y: (arry_kwh[i + 1].y - arry_kwh[i].y) }; } console.log(JSON.stringify(arry_kwh_diff, null, 2));
The returned log is
[ { "label": "2020-04-01 11:44:13NaN", "y": 50 }, { "label": "2020-04-01 11:45:49NaN", "y": 25 }, { "label": "2020-04-01 13:11:24NaN", "y": 25 }, { "label": "2020-04-02 10:19:08NaN", "y": 50 }, { "label": "2020-04-02 11:24:38NaN", "y": 50 }, { "label": "2020-04-04 12:24:38NaN", "y": 100 }, { "label": "2020-04-05 13:49:38NaN", "y": 50 }, { "label": "2020-04-05 14:36:38NaN", "y": 100 }, { "label": "2020-04-05 14:24:38NaN", "y": 50 }, { "label": "2020-04-08 09:20:41NaN", "y": 35 }, { "label": "2020-04-08 10:09:38NaN", "y": 55 }, { "label": "2020-04-08 11:35:33NaN", "y": 35 }, { "label": "2020-04-08 13:24:38NaN", "y": 50 } ]
The $dataPointskWh
have
Array ( [0] => Array ( [label] => 2020-04-01 11:44:13 [y] => 100 ) [1] => Array ( [label] => 2020-04-01 11:45:49 [y] => 150 ) [2] => Array ( [label] => 2020-04-01 13:11:24 [y] => 175 ) [3] => Array ( [label] => 2020-04-02 10:19:08 [y] => 200 ) [4] => Array ( [label] => 2020-04-02 11:24:38 [y] => 250 ) [5] => Array ( [label] => 2020-04-04 12:24:38 [y] => 300 ) [6] => Array ( [label] => 2020-04-05 13:49:38 [y] => 400 ) [7] => Array ( [label] => 2020-04-05 14:36:38 [y] => 450 ) [8] => Array ( [label] => 2020-04-05 14:24:38 [y] => 550 ) [9] => Array ( [label] => 2020-04-08 09:20:41 [y] => 600 ) [10] => Array ( [label] => 2020-04-08 10:09:38 [y] => 635 ) [11] => Array ( [label] => 2020-04-08 11:35:33 [y] => 690 ) [12] => Array ( [label] => 2020-04-08 13:24:38 [y] => 725 ) [13] => Array ( [label] => 2020-04-08 17:12:35 [y] => 775 ) )
How can I remove NaN
from date-time? I have looked into this issue but couldn’t find any solution.
Any help would be highly appreciated.
Advertisement
Answer
You can easily do it by mapping the label
attribute and remove the last 3 characters
like this
data = data.map(el => el.label.substr(0, el.label.length - 3))
Notice: it is recommended the first answer in case of that all objects have NaN at the end of line otherwise use the second solution
or by another way, you can replace
NaN with ""
like this one
data = data.map(el => el.label.replace('NaN', ''))