javascript - How do I filter and structure this JSON? -
i have array of json objects known events, has fields eventid, eventname , importantly dateofevent.
[{event1}, {event2}, {event3}];
i need iterate on array of events , filter out events on same day. obtain new object this.
"days": [ { "date": "11-05-2015", "events": [ {"eventid": 1, ...}, {"eventid": 2, ...}, {"eventid": 3, ...}, {"eventid": 4, ...}, ] },
how can efficiently javascript or angular?
you can create new object dates keys, , values arrays contain events. after have date => events map, convert array in structure want.
assuming original data in variable called "events":
var events = [{event 1}, {event 2}...]; var daysmap = {}; var days = []; events.map(function(event) { if (days[event.dateofevent] === undefined) { days[event.dateofevent] = []; } days[event.dateofevent].push(event); }); (var day in daysmap) { days.push({ date: day, events: daysmap[day] }); }
Comments
Post a Comment