-1

I'm trying to update 2d array but it's not working here. Please check the below code

var ksatodayvalue = [];

function pad2(n) { return n < 10 ? '0' + n : n }
var dt = new Date();
currentyear = dt.getFullYear().toString();
currentmonth = pad2(dt.getMonth() + 1);
currentday = pad2(dt.getDate());

   $.getJSON(jsonrequesturl, function (data) {
        $.each(data, function (index, d) {
            ksatodayvalue.push("[Date.UTC("+currentyear+", "+currentmonth+", "+currentday-1+", "+d.time.split(':')[0]+", "+d.time.split(':')[1]+", "+d.time.split(':')[2]+"),3]");
        });

I want array like that.

var ksatodayvalue = [[Date.UTC(2014, 03, 18, 23, 45, 00),3],[Date.UTC(2014, 03, 18, 23, 30, 00),4],[Date.UTC(2014, 03, 18, 23, 15, 00),6],[Date.UTC(2014, 03, 18, 23, 00, 00),8]];
Rohit Khurana
  • 269
  • 3
  • 8
  • 18
  • Related? http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – elclanrs Mar 19 '14 at 01:57
  • Also, you're pushing a string not an array... What kind of object is a `Date.UTC(...)`. Your code doesn't make much sense. I think you're confusing a few things here... – elclanrs Mar 19 '14 at 02:00
  • Like elclanrs said, you're just pushing a string into the `ksatodayvalue`. You need to push an array containing the appropriate objects/numbers. Also, do you have `currentyear`, `currentmonth`, and `currentday` defined somewhere? – danasilver Mar 19 '14 at 02:02
  • i'm passing array to highchart.. – Rohit Khurana Mar 19 '14 at 02:03

2 Answers2

0

a) You are creating a string, and probably want an object. b) You probably want values from the incoming data, and not from local variables. Try something along these lines:

ksatodayvalue.push([Date.UTC(this.currentyear, this.currentmonth, this.currentday-1, d.time.split(':')[0], d.time.split(':')[1], d.time.split(':')[2]),3]);
sabof
  • 8,062
  • 4
  • 28
  • 52
0
var ksatodayvalue = [];
$.getJSON(jsonrequesturl, function(data) {
  $.each(data, function(index, d) {
    ksatodayvalue.push([
        Date.UTC(
          this.currentyear,
          this.currentmonth,
          this.currentday-1,
          d.time.split(':')[0],
          d.time.split(':')[1],
          d.time.split(':')[2]
        ), 3
    ]);

  });
}).done(function() {
  console.log(ksatodayvalue);
});
Gonzalo Bahamondez
  • 1,371
  • 1
  • 16
  • 37
  • it's not working here..showing result like this :- 1397864700000,3,1397863800000,3 – Rohit Khurana Mar 19 '14 at 02:14
  • i need this :- [[Date.UTC(2014, 03, 18, 23, 45, 00),3],[Date.UTC(2014, 03, 18, 23, 30, 00),4],[Date.UTC(2014, 03, 18, 23, 15, 00),6],[Date.UTC(2014, 03, 18, 23, 00, 00),8]]; – Rohit Khurana Mar 19 '14 at 02:15
  • You need this as string? take a look , on Date.UTC function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC. – Gonzalo Bahamondez Mar 19 '14 at 02:20