有条件地使用JavaScript

时间:2018-09-11 13:12:23

标签: javascript

我有一个对象数组:

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 AMsr_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
        }
];

有什么建议吗?

4 个答案:

答案 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.keysArray.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));