2

I want to use SectionList in React Native. Datas that SectionList needs to render looks like this:

sections=[
 {
   date: 'May 24 2019', 
   data: [
     {
       id:1,
       msg: 'msg1',
       date: 'May 24 2019'
     },
     {
       id:2,
       msg: 'msg2',
       date: 'May 24 2019'
     },
 ]},
 {
   date: 'May 25 2019', 
   data: [
     {
       id:3,
       msg: 'msg1',
       date: 'May 25 2019'
     },
     {
       id:4,
       msg: 'msg2',
       date: 'May 25 2019'
     },
 ]},
];

But data that I receive from server looks like this:

data:[
  {
       id:1,
       msg: 'msg1',
       date: 'May 24 2019'
  },
  {
       id:2,
       msg: 'msg2',
       date: 'May 24 2019'
  },
  {
       id:3,
       msg: 'msg1',
       date: 'May 25 2019'
  },
  {
       id:4,
       msg: 'msg1',
       date: 'May 25 2019'
  },
];

So how can I convert "data" to look like "sections". I appreciate your help. And sorry for my English.

1
  • The wrapping object literals {} in your output are wrong. They should be removed Commented May 23, 2019 at 7:05

3 Answers 3

2

You could use Array.reduce to create the actual grouping and then extract the values via Object.values:

let data = [ { id:1, msg: 'msg1', date: 'May 24 2019' }, { id:2, msg: 'msg2', date: 'May 24 2019' }, { id:3, msg: 'msg1', date: 'May 25 2019' }, { id:4, msg: 'msg1', date: 'May 25 2019' }, ];

let result = data.reduce((r,{ date, ...other}) => {
  r[date] = r[date] || { date, data: [] }
  r[date].data = [...r[date].data, { date, ...other }]
  return r
}, {})

console.log(Object.values(result))

Sign up to request clarification or add additional context in comments.

Comments

1

You can do that in following steps:

  • Use reduce() to build up an an object whose keys will different dates and values will be array containing items
  • Then get values of that object using Object.values()
  • Then use map() on that and add additional property date and return the object.

const data = [ { id:1, msg: 'msg1', date: 'May 24 2019' }, { id:2, msg: 'msg2', date: 'May 24 2019' }, { id:3, msg: 'msg1', date: 'May 25 2019' }, { id:4, msg: 'msg1', date: 'May 25 2019' }, ]

const res = Object.values(
                  data.reduce((ac,a) => (ac[a.date] = (ac[a.date] || []).concat(a),ac),{})
             ).map(x => ({data:[...x],date:x[0].date}))

console.log(res)

Comments

0

let data = [
    {
        id: 1,
        msg: 'msg1',
        date: 'May 24 2019'
    },
    {
        id: 2,
        msg: 'msg2',
        date: 'May 24 2019'
    },
    {
        id: 3,
        msg: 'msg1',
        date: 'May 25 2019'
    },
    {
        id: 4,
        msg: 'msg1',
        date: 'May 25 2019'
    },
];

let result = {}

data.map(val=>{
    if(result[val.date]){
        return result[val.date] = {...result[val.date], value: [...result[val.date].value, val]}
    }else{
        return result[val.date] = {date: val.date, value: [val]}
    }
})

console.log(Object.values(result));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.