let frequentarr = [{accountID: "-Ks8mWcr2x08gEbSCSuJ", age: 67, gender: "female", brand: "Akira", date: "2017/01/09"},
{accountID: "-Ks8mWh4j7fv7XaQyygJ", age: 11, gender: "female", brand: "Akira", date: "2017/01/14"},
{accountID: "-Ks8mWh4j7fv7XaQyygJ", age: 11, gender: "female", brand: "Akira", date: "2017/01/14"},
{accountID: "-Ks8mWayOns5WnK0xu3e", age: 62, gender: "male", brand: "Akira", date: "2017/01/15"},
{accountID: "-Ks8mWYnb4fu1AT9vR8X", age: 40, gender: "male", brand: "Akira", date: "2017/01/21"},
{accountID: "-Ks8mWf0deAAVlOSb2qS", age: 21, gender: "male", brand: "Akira", date: "2017/02/08"},
{accountID: "-Ks8mWbH4YQhXg9DZvjG", age: 12, gender: "male", brand: "Akira", date: "2017/02/18"},
{accountID: "-Ks8mWZpiUOt8aXyEY22", age: 56, gender: "female", brand: "Akira", date: "2017/02/29"},
{accountID: "-Ks8mW_HvNJ3RKCz6QZk", age: 24, gender: "male", brand: "Akira", date: "2017/03/03"},
{accountID: "-Ks8mWgBmSBh6LrsLMU4", age: 22, gender: "male", brand: "Akira", date: "2017/03/17"},
{accountID: "-Ks8mWgceHTuJsBObl_C", age: 66, gender: "female", brand: "Akira", date: "2017/03/25"},
{accountID: "-Ks8mWYhd7WuUGJMUTzx", age: 62, gender: "female", brand: "Akira", date: "2017/04/03"},
{accountID: "-Ks8mWhQ-TsvAbvS0Sk2", age: 59, gender: "male", brand: "Akira", date: "2017/05/18"},
{accountID: "-Ks8mWY4PQ3stDWTlriB", age: 39, gender: "male", brand: "Akira", date: "2017/06/14"},
{accountID: "-Ks8mWYMZw8YTs6P-UCk", age: 19, gender: "male", brand: "Akira", date: "2017/07/03"},
{accountID: "-Ks8mWcm1zYkSQN2fD9c", age: 31, gender: "male", brand: "Akira", date: "2017/07/10"},
{accountID: "-Ks8mWalbiAq35e10lZy", age: 24, gender: "male", brand: "Akira", date: "2017/07/14"},
{accountID: "-Ks8mWjmbuBtmzzJ2IPu", age: 36, gender: "female", brand: "Akira", date: "2017/07/24"},
{accountID: "-Ks8mWjmbuBtmzzJ2IPu", age: 36, gender: "female", brand: "Akira", date: "2017/07/24"},
{accountID: "-Ks8mWbNJ53rNuspHlQe", age: 73, gender: "male", brand: "Akira", date: "2017/08/17"},
{accountID: "-Ks8mWcvM1-w0GAl-eHs", age: 63, gender: "male", brand: "Akira", date: "2017/10/01"},
{accountID: "-Ks8mWhvTEp83PziAxpp", age: 16, gender: "male", brand: "Akira", date: "2017/10/14"},
{accountID: "-Ks8mWdGfSrC-5n8ywNs", age: 70, gender: "female", brand: "Akira", date: "2017/10/18"},
{accountID: "-Ks8mWdf0LfFKWH8-YBF", age: 29, gender: "male", brand: "Akira", date: "2017/12/03"},
{accountID: "-Ks8mWhtA-3ExwVUWZC_", age: 15, gender: "female", brand: "Akira", date: "2017/12/07"},
{accountID: "-Ks8mWX4R6inD83AO4rZ", age: 47, gender: "female", brand: "Akira", date: "2017/12/19"},
{accountID: "-Ks8mWh4j7fv7XaQyygJ", age: 11, gender: "female", brand: "Akira", date: "2017/12/25"},
{accountID: "-Ks8mWadHFzJ1VGmN60k", age: 26, gender: "male", brand: "Akira", date: "2017/12/28"}];
let ageGroups = ['80-1000', '70-79', '60-69', '50-59', '40-49', '30-39', '20-29', '10-19', '0-9'];
function getAgeRange(age) {
return ageGroups.find(e => {
let s = e.split('-');
return age >= s[0] && age <= s[1];
})
}
let m = frequentarr.sort((a,b) => new Date(b.date) - new Date(a.date)).reduce((a,b) => {
let r = getAgeRange(b.age);
return a.set(r, a.has(r) ? a.get(r).concat(b) : [b])
}, new Map);
console.log(m);
あなたは彼らが年齢によって並べ替えられていますので、あなたは_Controlのbreak_を使ってこれを実装することができます。また、年齢別にデータをサブアレイに分割することもできます。 – CBroe
FYIでは、日付形式は文字列比較を使用して「ソート可能」なので、必ずソートコールバック関数でDateオブジェクトを作成する必要はありません。物事をスピードアップさせることができます。特に、大量のデータを扱う場合にはそうです。 – CBroe
'da == db'は常に** falseになります。それらは異なるオブジェクトです(同じdatを含んでいても)。日付順に並べ替えるには、単に '新しい日付を返す(a.date) - 新しい日付(b.date);'( '--'はその日付を数字に強制するため)です。しかし、あなたのコードが日付のグループ*を使って何をするのかは不明です。 –