How do I get my data into the correct format for React Native's SectionList?

Viewed 907

I have an allPosts array that looks like this.

const allPosts = [
    {
      date: '2017-06-06',
      imageUrl: 'image1_06-06',
    },
    {
      date: '2017-06-06',
      imageUrl: 'image2_06-06',
    },
    {
      date: '2017-06-07',
      imageUrl: 'image1_06-07',
    },
    {
      date: '2017-06-07',
      imageUrl: 'image2_06-07',
    }
];

It needs to organized like this for React Native's SectionList.

const postsByDate = [
    {
      data: [{ imageUrl: 'image1_06-06' }, { imageUrl: 'image2_06-06' }],
      key: '2017-06-06',
    },
    {
      data: [{ imageUrl: 'image1_06-07' }, { imageUrl: 'image2_06-07' }],
      key: '2017-06-07',
    },
];

I'm having a hard time figuring how to deep-push the data for each dated key.

If it matters allPosts actually has more than imageUrl but I removed the extra data for simplicity.

3 Answers
Related