使用JavaScript或Jquery我有以下基于开始日期(坐标)排序的数组:
[{
elem: '<div id="task7">',
startDate: 864,
endDate: 999,
order: 0
},
{
elem: '<div id="task6">',
startDate: 918,
endDate: 1053,
order: 1
},
{
elem: '<div id="task5">',
startDate: 945,
endDate: 1269,
order: 2
},
{
elem: '<div id="task4">',
startDate: 972,
endDate: 1107,
order: 3
},
{
elem: '<div id="task8">',
startDate: 1026,
endDate: 1161,
order: 4
}]
如果一个字符串的“startDate”大于任何其他字符串的“endDate”,我想将“order”值更新为参数为true的compare字符串。
以下是我尝试输出的示例,其中字符串4的“顺序”为0(与字符串0相同):
[{
elem: '<div id="task7">',
startDate: 864,
endDate: 999,
order: 0
},
{
elem: '<div id="task6">',
startDate: 918,
endDate: 1053,
order: 1
},
{
elem: '<div id="task5">',
startDate: 945,
endDate: 1269,
order: 2
},
{
elem: '<div id="task4">',
startDate: 972,
endDate: 1107,
order: 3
},
{
elem: '<div id="task8">',
startDate: 1026,
endDate: 1161,
order: 0
}]
答案 0 :(得分:1)
这样的事可能
var arr = [
{elem: '<div id="task7">', startDate: 864, endDate: 999, order: 0},
{elem: '<div id="task6">', startDate: 918, endDate: 1053, order: 1},
{elem: '<div id="task5">', startDate: 945, endDate: 1269, order: 2},
{elem: '<div id="task4">', startDate: 972, endDate: 1107, order: 3},
{elem: '<div id="task8">', startDate: 1026, endDate: 1161, order: 4}
];
arr.forEach( function(start){
var less = arr.filter(function(item){ return start.startDate > item.endDate})[0];
if (less) start.order = less.order;
} );
console.log(arr);