0

I have this data:

let array = [
  {name:"20220503",num:"0900"}
  {name:"20220503",num:"1500"},{name:"20220503",num:"1700"},
  {name:"20220504",num:"2000"}, {name:"20220505",num:"1100"}, 
  {name:"20220505",num:"1300"}
];
    

I want to make it become like this:

{ 
'20220503':
   { 
     list: [ '0900', '1500', '1700' ],
     data: [
     {
       label: 0900
       value: 0900
     }, 
     {
       label: 1500
       value: 1500
     }, 
     {
       label: 1700
       value: 1700
     }
    ]
   },

'20220504': 
    { 
        list: [ '2000' ],
        data: [
         {
           label: 2000
           value: 2000
         },
        ]
    },

'20220505': 
    { 
    list: [ '1100', '1300' ], 
    data: [
         {
           label: 1100
           value: 1100
         }, 
         {
           label: 1300
           value: 1300
         },
       ]
    }
}

This is what i have done so far:

let array = [
   {date:"20220503",time:"0900"},
    {date:"20220503",time:"1500"},
    {date:"20220503",time:"1700"},
    {date:"20220504",time:"2000"},
    {date:"20220505",time:"1100"},
    {date:"20220505",time:"1300"},
    {date:"20220506",time:"100"},
    {date:"20220507",time:"5300"},
    {date:"20220507",time:"90100"},
    {date:"20220509",time:"2100"}
]
let result = array.reduce((acc,{date,time}) => {
    acc[date] = acc[date] || {list:[], data:[]}
    acc[date].list.push(time)
    acc[date].data.push(time)
    
    return acc;
},{})

console.log(result)

But i only get result like this:

{
  '20220503': {
    list: [
      '0900',
      '1500',
      '1700'
    ],
    data: [
      '0900',
      '1500',
      '1700'
    ]
  },
  '20220504': {
    list: [
      '2000'
    ],
    data: [
      '2000'
    ]
  },
  '20220505': {
    list: [
      '1100',
      '1300'
    ],
    data: [
      '1100',
      '1300'
    ]
  },
}

I am using Javascript

2
  • 1
    As far as I can tell all that's missing is changing acc[date].data.push(time) into acc[date].data.push({ label: time, value: time }), no? Commented Aug 24, 2022 at 7:23
  • Yes, you are right. Thank you so much. Now I understand how to insert the label and value. @ChrisG Commented Aug 24, 2022 at 8:04

1 Answer 1

1

As @ChrisG already pointed out, you just have to change the acc[date].data.push line. And parse the strings into actual numbers:

let array = [
   {date:"20220503",time:"0900"},
    {date:"20220503",time:"1500"},
    {date:"20220503",time:"1700"},
    {date:"20220504",time:"2000"},
    {date:"20220505",time:"1100"},
    {date:"20220505",time:"1300"},
    {date:"20220506",time:"100"},
    {date:"20220507",time:"5300"},
    {date:"20220507",time:"90100"},
    {date:"20220509",time:"2100"}
]
let result = array.reduce((acc,{date,time}) => {
    acc[date] = acc[date] || {list:[], data:[]}
    acc[date].list.push(time)
    acc[date].data.push({ label: Number(time), value: Number(time) }); // <-- here
    
    return acc;
},{})

console.log(result)

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

2 Comments

Yes this is correct, thank you so much :)
In that case, please accept the answer so that others can benefit if they have the same problem.

Your Answer

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