last day (15 days later) » 

10:32 PM
1
A: Javascript array of arrays query

BarmarGiving multiple arguments to push is just short for calling push separately with each argument, it doesn't collect them together in an array. You need to make them an object first: function plotgraph1(filter1) { var processed_json = $j.map(filter1, function(obj) { return {date: obj[0...

 
Yes, the array is defined at first by me also. Usig $j.each gives me an "undefined" array completely. What's wrong here?
 
The arguments to the iteration function for $.each are in the opposite order. However, I just changed it to use $.map and use the return value.
 
So I just used this, and it gave me the exact same thing I am printing, graph is plotted but console has same format as posted in my problem (ie. date,metric,date,metric and so on) So I tried this : return [{"date":obj[0]}, {"metric":obj[1]}]; This gives in console : 0: Object date: "2014-06-14T18:30:00Z" proto: Object 1: Object metric: 4044 proto: Object 2: Object date: "2014-06-15T18:30:00Z" proto: Object 3: Object metric: 5030 proto: Object This is better, but I need date and metric (corresponding) in one object, then 2nd object with date and metric together and so on
 
I've changed my answer to return an object.
You can't return two things in one return call. return {blah1}, {blah2} is the same as just return {blah2} because of the way the comma operator works.
 
Barmar, this did the trick I am getting both date and metric together in one object. But the charts are not coming still and I just checked back, it needs to be an array ( not an object!) I'm really sorry for irritating. But if you could just convert this to an array, this would be solved I think. Thanks a lot anyways !
But, whenever I convert this to array using push, the date and metric separate out as two different parts! How do I make them come inside one array?
Hey barmar, there?
Object wise, you have solved my problem.. thanks a lot! :)
But I just realized, we need it in arrays for the highcharts.js to print it. Can you please tell me if we can do that and have date and metric together. I just tried it using this, but again only date and only metric are coming in array separately and not together.
var processed_json = new Array();

$j.map(filter1, function(obj) {
processed_json.push ({0: obj[0]}, {1: obj[1]});
 
10:43 PM
My original answer put it in an array, I don't know why it didn't work. I changed it to an object because you said that was what you needed.
It should be processed_json.push([obj[0], obj[1]])
 
Damn, I wrote this : processed_json.push([obj[0], obj[1]]) and the graph came as it's in an array. But I again came back to my start point ie data is printed in one array only, I think it has to be an array of arrays!
Data example : 0: "2014-06-14T18:30:00Z"
1: 4044
2: "2014-06-15T18:30:00Z"
3: 5030
4: "2014-06-16T18:30:00Z"
5: 4913
6: "2014-06-17T18:30:00Z"
7: 4985
But, 0 and 1 in one array, 2 and 3 in second array and so on is the needed output for the dates to come properly in xaxis of highcharst
highcharts*
You had done this successfully with objects.
Let me know if you can also do this with an array!
 
Could you show what the original filter1 variable contains?
It seems like it's already an array of arrays, the way you're using it with $.map.
Ugh, I just noticed this in the description of $.map: A returned array will be flattened into the resulting array. That explains the problem.
But it works with processed_json.push([obj[0], obj[1]]). See jsfiddle.net/c3TRx/3
 

  last day (15 days later) »