【问题标题】:Rearrange array of objects by date按日期重新排列对象数组
【发布时间】:2020-06-15 18:05:25
【问题描述】:

我从 API 收到所有活动和已完成的任务。我想调整此对象以使用 SectionList (React-Native),使用每个日期字段 (dataCadastro)按天分隔。例如https://reactnative.dev/docs/sectionlist

{
  active: [
    {
      id: 1,
      dataCadastro: '2020-06-15T14:33:45.4807609+00:00',
      status: 1,
      description: '',

    },
    {
      id: 2,
      dataCadastro: '2020-06-15T15:33:45.4807609+00:00',
      status: 1,
      description: '',

    },
  ],
  completed: [
    {
      id: 3,
      dataCadastro: '2020-05-19T14:33:45.4807609+00:00',
      status: 1,
      description: '',

    },
    {
      id: 4,
      dataCadastro: '2020-05-19T15:33:45.4807609+00:00',
      status: 1,
      description: '',

    }
  ]
}

这样的结果会很棒:

const dataTest = [
    {
      title: '2020-06-15', // All tasks for the 15th of June.
      data: [
        {
          id: 1,
          dataCadastro: '2020-06-15T14:33:45.4807609+00:00',
          status: 1,
          description: '',

        },
       {
          id: 2,
          dataCadastro: '2020-06-15T15:33:45.4807609+00:00',
          status: 1,
          description: '',

        }
      ],
    }, {...}  
  ];

【问题讨论】:

  • 你自己有没有尝试过?

标签: javascript arrays react-native object


【解决方案1】:

首先要做的是将所有数据合并在一起。

const allTasks = data.active.concat(data.completed);

然后你需要按日期聚合它们。

const dataObject = allTasks.reduce((dataObject, task) => {
    const date = new Date(task.dataCadastro.toLocaleDateString());

    if (!dataObject[date]) dataObject[date] = { title: date, data: [] };
    dataObject[date].data.push[task];
    return dataObject;
}, {});

然后需要将数据对象转换成数组,并按日期排序。

const data = Object.values(dataObject).sort((a, b) => a.title - b.title);

【讨论】:

    【解决方案2】:

    如果您在activecompleted 中的数据属于同一类型,那么您可以通过获取条目来使用它:

    var obj = { active: [ { id: 1, dataCadastro: '2020-06-15T14:33:45.4807609+00:00', status: 1, description: '', }, { id: 2, dataCadastro: '2020-06-15T15:33:45.4807609+00:00', status: 1, description: '', }, ], completed: [ { id: 3, dataCadastro: '2020-05-19T14:33:45.4807609+00:00', status: 1, description: '', }, { id: 4, dataCadastro: '2020-05-19T15:33:45.4807609+00:00', status: 1, description: '', } ]};
    
    var result = Object.entries(obj).map(([_,data])=>({title: data[0].dataCadastro, data }));
    
    console.log(result);

    否则你可以去reduce:

    var obj = { active: [ { id: 1, dataCadastro: '2020-06-15T14:33:45.4807609+00:00', status: 1, description: '', }, { id: 2, dataCadastro: '2020-06-15T15:33:45.4807609+00:00', status: 1, description: '', }, ], completed: [ { id: 3, dataCadastro: '2020-05-19T14:33:45.4807609+00:00', status: 1, description: '', }, { id: 4, dataCadastro: '2020-05-19T15:33:45.4807609+00:00', status: 1, description: '', } ]};
    
    var result = Object.entries(obj).reduce((acc, [_, data])=>{
      data.forEach(elem=>{
         title = elem.dataCadastro.slice(0,10)
         acc[title] = acc[title] || {title, data:[]};
         acc[title].data.push(elem);
       });
     return acc;
    },{});
    
    console.log(Object.values(result));

    【讨论】:

      猜你喜欢
      • 2017-05-21
      • 1970-01-01
      • 2021-10-14
      • 2015-02-25
      • 2021-07-05
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多