2016-12-04 97 views
1

我試圖重新排序如下今天的日期數組的數組:訂購基於當前日期

[ 
    { 
     "index": "0", 
     "day": "Monday", 
     "food": "Salad" 
    }, 
    { 
     "index": "1", 
     "day": "Friday", 
     "food": "Pasta" 
    }, 
    { 
     "index": "2", 
     "day": "Sunday", 
     "food": "Pasta" 
    }, 
    { 
     "index": "3", 
     "day": "Thursday", 
     "food": "Pasta" 
    }, 
    { 
     "index": "4", 
     "day": "Tuesday", 
     "food": "Pasta" 
    }, 
    { 
     "index": "2", 
     "day": "Sunday", 
     "food": "Pasta" 
    }, 
    { 
     "index": "5", 
     "day": "Wednesday", 
     "food": "Pasta" 
    }, 
    { 
     "index": "6", 
     "day": "Saturday", 
     "food": "Pasta" 
    } 
] 

今天要說的是Saturday,訂貨後,由項目指數的預期成果是:6,2 2,0,4,5,3,1 如果今天是Tuesday,由索引輸出是:4,5,3,1,6,2,2,0

我使用moment.js,等遠遠沒有爲此創建一個非龐大的代碼。 最後,我想將代碼包裝在自定義orderBy過濾器中以用於ng-repeat指令。

+0

爲什麼'Sunday'出現兩次? –

+0

@miparnisari只是爲了說明可以在數組中複製對象。 – user1027620

+0

你能告訴我們你到目前爲止使用了什麼代碼嗎? – anurupr

回答

2

這裏是最小的解決方案,我想出了:

const DAYS = { 'Sunday': 0, 'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 5, 'Saturday': 6 }; 

// Get the relative index of a day using today as a reference. 
function relativeIndex(day) { 
    const TODAY = new Date().getDay(); 
    return (DAYS[day] - TODAY + 7) % 7; 
} 

// Sort a lit of data starting by today. 
function sort(data) { 
    return data.sort((a, b) => relativeIndex(a.day) - relativeIndex(b.day)); 
} 

這裏的關鍵是relativeIndex函數計算使用今天作爲參考的任何一天的指數。

+0

這就是爲什麼我在這裏。很好的答案! – synthet1c

1

今天排序是一個有點棘手,實現只使用排序。爲了克服它,我們創建了一個日期陣列,然後求助於當前日期在索引0處,然後根據排序的日期對日曆數組進行排序。

爲了避免多次計算sortedDays,它被包裝在一個閉包中,該閉包返回將對給定數組進行排序的函數。

它是不可變的,所以如果你不需要的話,你不需要改變原來的順序。

const calendar=[{index:"0",day:"Monday",food:"Salad"},{index:"1",day:"Friday",food:"Pasta"},{index:"2",day:"Sunday",food:"Pasta"},{index:"3",day:"Thursday",food:"Pasta"},{index:"4",day:"Tuesday",food:"Pasta"},{index:"2",day:"Sunday",food:"Pasta"},{index:"5",day:"Wednesday",food:"Pasta"},{index:"6",day:"Saturday",food:"Pasta"}]; 
 

 
const sortByToday = (function() { 
 
    
 
    const date = new Date() 
 
    const today = date.getDay() 
 
    
 
    const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] 
 

 
    const sortedDays = [ 
 
    ...days.slice(today), 
 
    ...days.slice(0, today) 
 
    ] 
 
    
 
    return function(calendar) { 
 
    return calendar.slice().sort((a, b) => { 
 
     return sortedDays.indexOf(a.day) > sortedDays.indexOf(b.day) 
 
    }) 
 
    } 
 
})() 
 

 
console.log(
 
    sortByToday(calendar) 
 
)

0

鑑於 '菜單' 作爲給定陣列,使用定製的比較來排序:

var map = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; 
var day = (new Date()).getDay(); 
menu.sort(function(a, b){ 
    var aDay = map.indexOf(a.day), bDay = map.indexOf(b.day); 
    return (aDay - bDay) * 
     (day <= Math.min(aDay, bDay) || day > Math.max(aDay, bDay) ? 1 : -1); 
}); 

for(var i=0; i<menu.length; ++i){ 
    console.log(menu[i].index); 
} 
0

使用日期()體操。向下滾動查看註釋代碼。

let arr = [ 
 
    { 
 
     "index": "0", 
 
     "day": "Monday", 
 
     "food": "Salad" 
 
    }, 
 
    { 
 
     "index": "1", 
 
     "day": "Friday", 
 
     "food": "Pasta" 
 
    }, 
 
    { 
 
     "index": "2", 
 
     "day": "Sunday", 
 
     "food": "Pasta" 
 
    }, 
 
    { 
 
     "index": "3", 
 
     "day": "Thursday", 
 
     "food": "Pasta" 
 
    }, 
 
    { 
 
     "index": "4", 
 
     "day": "Tuesday", 
 
     "food": "Pasta" 
 
    }, 
 
    { 
 
     "index": "2", 
 
     "day": "Sunday", 
 
     "food": "Pasta" 
 
    }, 
 
    { 
 
     "index": "5", 
 
     "day": "Wednesday", 
 
     "food": "Pasta" 
 
    }, 
 
    { 
 
     "index": "6", 
 
     "day": "Saturday", 
 
     "food": "Pasta" 
 
    } 
 
] 
 

 
// first create an array to label days 
 
let dayOfWeekLabels = [ 
 
    'Sunday', 
 
    'Monday', 
 
    'Tuesday', 
 
    'Wednesday', 
 
    'Thursday', 
 
    'Friday', 
 
    'Saturday' 
 
]; 
 

 
let days = []; 
 
let runningDate = new Date(); 
 

 
// optional offset for debugging/testing 
 
//runningDate.setDate(runningDate.getDate()+1); 
 

 
// now create an array that rearranges the first array 
 
// according to what day it is today 
 
dayOfWeekLabels.forEach((label,i) => { 
 
    days.push(dayOfWeekLabels[new Date(runningDate).getDay()]); 
 
    runningDate.setDate(runningDate.getDate()+1); 
 
}); 
 

 
// now enhance the original array with an "order" value 
 
let orderedArr = arr.map(obj => { 
 
    obj.order = days.indexOf(obj.day); 
 
    return obj; 
 
}); 
 

 
// now sort based on the "order" value 
 
orderedArr.sort((a,b) => { 
 
    return (a.order - b.order); 
 
}); 
 
/* 
 
console.log(orderedArr); 
 
*/ 
 
document.getElementById('output').innerHTML = JSON.stringify(orderedArr,null," ");
<pre><code class="json" id="output"></code></pre>