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?

4

3 回答 3

4

您可以使用sort()将自定义排序器函数作为参数传递的方法(见下文)。
为了获取每个数组的第一个元素对应的日期的星期几(例如“2013-04-15”),您可以使用 Date 的getDay()函数。

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);

注意
getDay()返回 0 到 6(含)之间的整数,对应于星期天到星期六(星期日为 0,星期一为 1...)。

如果您想按星期几而不是排序对它们进行分类,您可以使用以下内容:

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);

另请参阅这个简短的演示

于 2013-05-15T13:23:33.117 回答
2

您可以sort按照 VisioN 的建议使用该功能并传递您自己的比较策略。像这样的东西应该适合你:

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
});

有关排序的更多信息:http: //www.w3schools.com/jsref/jsref_sort.asp

关于我的“战略对象”评论,这里有更多信息:http ://en.wikipedia.org/wiki/Strategy_pattern

于 2013-05-15T13:16:44.907 回答
1

如果目标是为一周中的每一天创建一个单独的数组,则不需要排序

 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"]]
]"
于 2013-05-15T14:00:16.683 回答