dav*_*o12 6 javascript arrays grouping json
我想转换showtimesData为showtimesByLocationByDate
任何想法,如何不使用任何第三方库,只使用纯JavaScript?否则,我可以使用哪个第三方库?
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"]
}
]
},
];
Run Code Online (Sandbox Code Playgroud)
该提案的特点是仅Array.prototype.reduce()使用一个临时对象来引用数组项。
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" }, ],
showtimesByLocationByDate = showtimesData.reduce(function (r, a) {
var o;
if (!(a.location in r.obj)) {
o = { location: a.location, dates: [] };
r.obj[a.location] = { dates: o.dates };
r.array.push(o);
}
if (!(a.date in r.obj[a.location])) {
o = { date: a.date, times: [] };
r.obj[a.location].dates.push(o);
r.obj[a.location][a.date] = o.times;
}
r.obj[a.location][a.date].push(a.time);
return r;
}, { array: [], obj: {} }).array;
document.write('<pre>' + JSON.stringify(showtimesByLocationByDate, 0, 4) + '</pre>');Run Code Online (Sandbox Code Playgroud)
奖励:具有给定数据结构的通用版本
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" }, ],
structure = [
{ key: 'location', data: 'dates' },
{ key: 'date', data: 'times' },
{ key: 'time' }
],
showtimesByLocationByDate = showtimesData.reduce(function (r, a) {
var properties = structure.slice(),
lastKey = properties.pop().key;
properties.reduce(function (rr, b) {
var o = {},
p = {},
key = b.key,
value = a[key],
array = b.data;
if (!(value in rr.obj)) {
o[key] = value;
o[array] = [];
p[array] = o[array];
rr.obj[value] = p;
rr.array.push(o);
}
return { array: rr.obj[value][array], obj: rr.obj[value] };
}, r).array.push(a[lastKey]);
return r;
}, { array: [], obj: {} }).array;
document.write('<pre>' + JSON.stringify(showtimesByLocationByDate, 0, 4) + '</pre>');Run Code Online (Sandbox Code Playgroud)