i want convert showtimesdata
showtimesbylocationbydate
idea how without using third party library , using pure javascript? otherwise, third party library can use this?
var showtimesdata = [ {"location":"location1", "date":"31-12-2016", "time":"1:00"}, {"location":"location1", "date":"31-12-2016", "time":"2:00"}, {"location":"location1", "date":"01-01-2017", "time":"3:00"}, {"location":"location1", "date":"01-01-2017", "time":"4:00"}, {"location":"location2", "date":"31-12-2016", "time":"1:00"}, {"location":"location2", "date":"31-12-2016", "time":"2:00"}, {"location":"location2", "date":"01-01-2017", "time":"3:00"}, {"location":"location2", "date":"01-01-2017", "time":"4:00"}, ]; var showtimesbylocationbydate = [ { "location":"location1", "dates":[ { "date":"31-12-2016", "times":["1:00","2:00"] }, { "date":"01-01-2017", "times":["3:00","4:00"] } ] }, { "location":"location2", "dates":[ { "date":"31-12-2016", "times":["1:00","2:00"] }, { "date":"01-01-2017", "times":["3:00","4:00"] } ] }, ];
i'd propose transformation:
var showtimesdata = [ {"location":"location1", "date":"31-12-2016", "time":"1:00"}, {"location":"location1", "date":"31-12-2016", "time":"2:00"}, {"location":"location1", "date":"01-01-2017", "time":"3:00"}, {"location":"location1", "date":"01-01-2017", "time":"4:00"}, {"location":"location2", "date":"31-12-2016", "time":"1:00"}, {"location":"location2", "date":"31-12-2016", "time":"2:00"}, {"location":"location2", "date":"01-01-2017", "time":"3:00"}, {"location":"location2", "date":"01-01-2017", "time":"4:00"}, ]; var transformed = showtimesdata.reduce(function(obj, show){ //var { location, date, time } = show; //if destructuring available var location = show.location, date = show.date, time = show.time, objlocation = obj[location] = obj[location] || { dates : { } }, dates = objlocation.dates, date = dates[date] = dates[date] || [ ]; date.push(time); return obj; }, {}); results.innerhtml = json.stringify(transformed, null, '\t');
<pre id="results"></pre>
but if want transform that, i'd propose grabing transformation , map proposed structure.
var showtimesdata = [ {"location":"location1", "date":"31-12-2016", "time":"1:00"}, {"location":"location1", "date":"31-12-2016", "time":"2:00"}, {"location":"location1", "date":"01-01-2017", "time":"3:00"}, {"location":"location1", "date":"01-01-2017", "time":"4:00"}, {"location":"location2", "date":"31-12-2016", "time":"1:00"}, {"location":"location2", "date":"31-12-2016", "time":"2:00"}, {"location":"location2", "date":"01-01-2017", "time":"3:00"}, {"location":"location2", "date":"01-01-2017", "time":"4:00"}, ]; var transformed = showtimesdata.reduce(function(obj, show){ //var { location, date, time } = show; //if destructuring available var location = show.location, date = show.date, time = show.time, objlocation = obj[location] = obj[location] || { dates : { } }, dates = objlocation.dates, date = dates[date] = dates[date] || [ ]; date.push(time); return obj; }, {}); var secondtransformed = object.keys(transformed).map(function(key){ var dates = transformed[key].dates, transformeddates = object.keys(dates).map(function(key){ return { date : key, times : dates[key] } }); return { location : key, dates : transformeddates } }); results.innerhtml = json.stringify(secondtransformed, null, '\t');
<pre id="results"></pre>
although there better ways (performance wise).
Comments
Post a Comment