4

I have the following variable (console.log(response)):

"['2013-04-15', 26]", "['2013-04-16', 10]", "['2013-04-17', 51]", "['2013-04-18', 46]", "['2013-04-19', 32]", "['2013-04-20', 50]", "['2013-04-21', 26]", "['2013-04-22', 31]", "['2013-04-23', 48]", "['2013-04-24', 821]", "['2013-04-25', 917]", "['2013-04-26', 949]", "['2013-04-27', 405]", "['2013-04-28', 593]", "['2013-04-29', 925]", "['2013-04-30', 877]", "['2013-05-01', 277]", "['2013-05-02', 112]", "['2013-05-03', 115]", "['2013-05-04', 62]", "['2013-05-05', 74]", "['2013-05-06', 76]", "['2013-05-07', 51]", "['2013-05-08', 93]", "['2013-05-09', 231]", "['2013-05-10', 350]", "['2013-05-11', 258]", "['2013-05-12', 0]", "['2013-05-13', 61]"

which I transform in an array of arrays in the following manner:

var json = response.replace(/"/g,'');
json = "[" + json + "]";
json = json.replace(/'/g,'"');
var myData = JSON.parse(json);

and I receive (console.log(myData)):

myData = [Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2]]

which I need to keep in this format for further usage. I want to know if is possible to sort the response by the day of the week? And also if is possible to store in a variable for example only the monday days, tuesday days in another one and so on? I have to use JQuery for this, is even a function that suits my needs?

Daniela costina Vaduva
  • 1,857
  • 5
  • 20
  • 22

3 Answers3

4

You can use method sort() passing your custom sorter function as parameter (see below).
In order to get the day-of-week of the date corresponding to each array's 1st element (e.g. "2013-04-15"), you can use Date's getDay() function.

var sorter = function(a, b) {
    /* The '.replace("-", "/")' part is for compatibility with Safari
       See also http://stackoverflow.com/questions/4310953/invalid-date-in-safari */
    var d1 = new Date(a[0].replace("-", "/")).getDay();
    var d2 = new Date(b[0].replace("-", "/")).getDay();
    return d1 - d2;
};
myData.sort(sorter);

NOTE:
getDay() returns an integer between 0 and 6 (inclusive), which correspond to days-of-week Sunday through Saturday (Sunday is 0, Monday is 1...).

If you want to classify them by day-of-week instead of sorting, you can use something like this:

function classifyByDayOfWeek(customArr) {
    var byDayOfWeek = [[], [], [], [], [], [], []];
    for (var i = 0; i < customArr.length; i++) {
        var day = new Date(customArr[i][0]).getDay();
        byDayOfWeek[day].push(customArr[i]);
    };
    return byDayOfWeek;
}
myData = classifyByDayOfWeek(myData);

See also this short demo.

gkalpak
  • 47,844
  • 8
  • 105
  • 118
2

You can use the sort function as VisioN suggested and pass your own comparison strategy. Something like this should work for you:

myData.sort(function(a,b)
{
  if ((a[0]>b[0]) && (a[1]>b[1])) return 1
  if ((a[0]==b[0]) && (a[1]==b[1])) return 0
  if ((a[0]<b[0]) && (a[1]<b[1])) return -1
});

more info about sort: http://www.w3schools.com/jsref/jsref_sort.asp

whit regards to my "Strategy Object" comment, here is some more information: http://en.wikipedia.org/wiki/Strategy_pattern

Mortalus
  • 10,574
  • 11
  • 67
  • 117
  • 1
    That code has all sorts of bugs in it (pun intended). What does it return if `a[0] > b[0]` and `a[1] <= b[1]`? What if `a[0] == b[0]` and `a[1] != b[1]`? Or `a[0] < b[0]` and `a[1] >= b[1]`? – Michael Geary May 16 '13 at 18:00
1

If the goal is to create a separate array for each day of the week, a sort is not necessary

 myData = ["['2013-04-15', 26]", "['2013-04-16', 10]", "['2013-04-17', 51]", "['2013-04-18', 46]", "['2013-04-19', 32]", "['2013-04-20', 50]", "['2013-04-21', 26]", "['2013-04-22', 31]", "['2013-04-23', 48]", "['2013-04-24', 821]", "['2013-04-25', 917]", "['2013-04-26', 949]", "['2013-04-27', 405]", "['2013-04-28', 593]", "['2013-04-29', 925]", "['2013-04-30', 877]", "['2013-05-01', 277]", "['2013-05-02', 112]", "['2013-05-03', 115]", "['2013-05-04', 62]", "['2013-05-05', 74]", "['2013-05-06', 76]", "['2013-05-07', 51]", "['2013-05-08', 93]", "['2013-05-09', 231]", "['2013-05-10', 350]", "['2013-05-11', 258]", "['2013-05-12', 0]", "['2013-05-13', 61]"];

myData = myData.map (function (v) { // convert to useable form
  v = v.match (/^\['(\d{4}-\d{2}-\d{2})',\s*(\d+)\]$/);
  return [v[1], v[2]];    
});

myDays = myData.reduce (function (days, v) {
  days[(new Date (v[0])).getDay ()].push (v);
  return days;
}, [[],[],[],[],[],[],[]]); // array of 7 arrays, one per day of week.

JSON.stringify (myDays);

-->

"[[["2013-04-21","26"],["2013-04-28","593"],["2013-05-05","74"],["2013-05-12","0"]],
  [["2013-04-15","26"],["2013-04-22","31"],["2013-04-29","925"],["2013-05-06","76"],["2013-05-13","61"]],
  [["2013-04-16","10"],["2013-04-23","48"],["2013-04-30","877"],["2013-05-07","51"]],
  [["2013-04-17","51"],["2013-04-24","821"],["2013-05-01","277"],["2013-05-08","93"]],
  [["2013-04-18","46"],["2013-04-25","917"],["2013-05-02","112"],["2013-05-09","231"]],
  [["2013-04-19","32"],["2013-04-26","949"],["2013-05-03","115"],["2013-05-10","350"]],
  [["2013-04-20","50"],["2013-04-27","405"],["2013-05-04","62"],["2013-05-11","258"]]
]"
HBP
  • 15,685
  • 6
  • 28
  • 34
  • And if I want one array for each of the day how I need to proceed? – Daniela costina Vaduva May 15 '13 at 14:04
  • Not sure I understand. Is that not the `myData` that produces `myDays`? Or can you have multiple data points in a single day.? – HBP May 15 '13 at 14:19
  • I would like something like `SundaysData = [["2013-04-21","26"],["2013-04-28","593"],["2013-05-05","74"],["2013-05-12","0"]]` `MondaysData = [["2013-04-15","26"],["2013-04-22","31"],["2013-04-29","925"],["2013-05-06","76"],["2013-05-13","61"]]` and so on. It is possible? – Daniela costina Vaduva May 15 '13 at 14:27
  • Is that not what my final output is? There are 7 lines each an array of entries. Each line corresponds to a day of the week, the first being Sunday. To map to your notation use `SundaysData = myDays[0]; Mondaysdata = mayDays[1];` etc. – HBP May 15 '13 at 14:33