Search code examples
node.jsreactjsreact-nativereact-native-calendarsagenda

how can I convert react-native data?


// i have to change my data to this form
items={{
'2021-04-20': [{name: 'item 1 - any js object'}],
'2012-04-21': [{name: 'item 2 - any js object'}]
}}

I am trying to show data below using react-native calendar library's agenda. And agenda needs the form of data like above.

['20210420': 'work', '20210421': 'sleep'] //the data i have

how can i convert my data by using react-native code?


Solution

  • Maybe this can help you!

    const data = {'20210420': 'work', '20210421': 'sleep'}
    const formattedData = {}
    Object
    .entries(data)
    .forEach(([key, value]) => formattedData[`${key.substr(0, 4)}-${key.substr(4, 2)}-${key.substr(6)}`]= [{ name: value }])
    console.log(formattedData)