我有一个对象数组:
const data = [
{
day: 'monday',
time: '10:00 AM',
type: 'start',
unit: 1
},
{
day: 'monday',
time: '11:30 AM',
type: 'end',
unit: 1
},
{
day: 'tuesday',
time: '01:00 PM',
type: 'start',
unit: 1
},
{
day: 'tuesday',
time: '06:00 PM',
type: 'end',
unit: 1
},
{
day: 'wednesday',
time: '10:00 AM',
type: 'start',
unit: 1
},
{
day: 'wednesday',
time: '06:00 PM',
type: 'end',
unit: 1
}
];
我需要将两个对象使用相同的'day'
键(例如day:'monday'
)组合成一个对象,如果对象包含type: 'start'
,则新对象将是'start_time': '10:00 AM'
对象,如果为'type': 'end'
,则它将是新对象中的'end_time': 11:30 AM
。 sr_no
以1开头,并以1递增。它(新对象的数组)应如下所示:
var newArray = [
{
sr_no: 1,
day: 'monday',
start_time: '10:00 AM',
end_time: '11:30 AM',
unit: 1
},
{
sr_no: 2,
day: 'tuesday',
start_time: '01:00 PM',
end_time: '06:00 PM',
unit: 1
},
{
sr_no: 3,
day: 'wednesday',
start_time: '10:00 AM',
end_time: '06:00 PM',
unit: 1
}
];
有什么建议吗?
答案 0 :(得分:0)
尽管如何确定sr_no
的方式尚不完全清楚,但是您可以使用map将数据转换为所需的数据。
const data = [
{
day: 'monday',
time: '10:00 AM',
type: 'start',
unit: 1
},
{
day: 'monday',
time: '11:30 AM',
type: 'end',
unit: 1
},
{
day: 'tuesday',
time: '01:00 PM',
type: 'start',
unit: 1
},
{
day: 'tuesday',
time: '06:00 PM',
type: 'end',
unit: 1
},
{
day: 'wednesday',
time: '10:00 AM',
type: 'start',
unit: 1
},
{
day: 'wednesday',
time: '06:00 PM',
type: 'end',
unit: 1
}
];
const transform = input => input
.map(dayData => dayData.day)
.map(day => input.filter(dayData => dayData.day === day))
.map(bothTimes => ({
day: bothTimes[0].day,
start_time: bothTimes.filter(dayData => dayData.type === 'start')[0].time,
end_time: bothTimes.filter(dayData => dayData.type === 'end')[0].time,
unit: bothTimes[0].unit
}))
const result = transform(data)
console.dir(result)
答案 1 :(得分:0)
如果您的数据将始终被排序,因此有一天总是相邻的,那么您可以轻松地使用一个旧的for循环:
const newArray = [];
for (let i = 0, n = 1; i < data.length; i += 2, n++) {
const sr_no = n;
const day = data[i].day;
const start_time = data[i].type === 'start' ? data[i].time : data[i + 1].time;
const end_time = data[i].type === 'end' ? data[i].time : data[i + 1].time;
const unit = data[i].unit;
newArray.push({
sr_no,
day,
start_time,
end_time,
unit
});
}
如果未对数据进行排序以使用这种循环,则只需在使用良好的sort()
回调之前对其进行排序。
答案 2 :(得分:0)
您可以使用Array.prototype.reduce
来按天汇总数据,并使用Object.keys
和Array.prototype.map
来将其格式化为所需的输出:
const data=[{day:'monday',time:'10:00 AM',type:'start',unit:1},{day:'monday',time:'11:30 AM',type:'end',unit:1},{day:'tuesday',time:'01:00 PM',type:'start',unit:1},{day:'tuesday',time:'06:00 PM',type:'end',unit:1},{day:'wednesday',time:'10:00 AM',type:'start',unit:1},{day:'wednesday',time:'06:00 PM',type:'end',unit:1}];
const grouped = data.reduce((all, {day, time, type, unit}) => {
if (!all.hasOwnProperty(day)) all[day] = { day, unit };
all[day][`${type}_time`] = time;
return all;
}, {});
const result = Object.keys(grouped).map((a,i) => ({sr_no: i + 1, ...grouped[a]}));
console.log(result);
答案 3 :(得分:0)
根据您的输出计算正确的sr_no。
const data = [
{
day: 'monday',
time: '10:00 AM',
type: 'start',
unit: 1
},
{
day: 'monday',
time: '11:30 AM',
type: 'end',
unit: 1
},
{
day: 'tuesday',
time: '01:00 PM',
type: 'start',
unit: 1
},
{
day: 'tuesday',
time: '06:00 PM',
type: 'end',
unit: 1
},
{
day: 'wednesday',
time: '10:00 AM',
type: 'start',
unit: 1
},
{
day: 'wednesday',
time: '06:00 PM',
type: 'end',
unit: 1
}
];
const update = (arr)=>{
const x = {}
const constructObj = (item,indx)=>{
const original = x[item.day] || {};
const key = item.type === 'start'? 'start_time':'end_time';
original.sr_no = Object.keys(x).indexOf(item.day)+1;
original[key] = item.time;
original.unit = item.unit;
return original;
}
arr.forEach((item,indx)=>{
x[item.day] = constructObj(item,indx);
})
return x;
}
console.log(update(data));