I have a list of simple objects that include date and I need to group them by the date. Unfortunately it doesn't seem to work.
Here is simplified code:
games = [
{date:"3/5/17", name:"game 1, 3"},
{date:"3/5/17", name:"game 2, 3"},
{date:"4/5/17", name:"game 3, 4"},
{date:"4/5/17", name:"game 4, 4"},
{date:"4/5/17", name:"game 5, 4"},
{date:"5/5/17", name:"game 6, 5"},
{date:"5/5/17", name:"game 7, 5"},
]
let games_by_date = []
games.map( (i)=>{
if(!games_by_date[i['date']])
games_by_date[i['date']]=[];
games_by_date[i['date']].push(i);
} )
console.log(games_by_date) // shows [3/5/17: Array[2], 4/5/17: Array[3], 5/5/17: Array[2]], but shows it as Array[0]
console.log(games_by_date[0]) // undefined
console.log(games_by_date[1]) // undefined
games_by_date.map( (g)=>console.log(g) ) // doesn't even gets there
If I do the same but use a different column, it's working correctly.
games = [
{date:"3/5/17", day:"3", name:"game 1, 3"},
{date:"3/5/17", day:"3", name:"game 2, 3"},
{date:"4/5/17", day:"4", name:"game 3, 4"},
{date:"4/5/17", day:"4", name:"game 4, 4"},
{date:"4/5/17", day:"4", name:"game 5, 4"},
{date:"5/5/17", day:"5", name:"game 6, 5"},
{date:"5/5/17", day:"5", name:"game 7, 5"},
]
let games_by_date = []
games.map( (i)=>{
if(!games_by_date[i['date']])
games_by_date[i['date']]=[];
games_by_date[i['date']].push(i);
} )
console.log(games_by_date)
console.log(games_by_date[0])
console.log(games_by_date[1])
games_by_date.map( (g, d)=>console.log(d, g) )
console.log("--------------------")
let games_by_day = []
games.map( (i)=>{
if(!games_by_day[i['day']])
games_by_day[i['day']]=[];
games_by_day[i['day']].push(i);
} )
console.log(games_by_day)
console.log(games_by_day[0])
console.log(games_by_day[1])
games_by_day.map( (g, d)=>console.log(d, g) )
So it seems to happen because of the type of keys I'm using.
Did you come across it and how did you solved it? Thanks