简体   繁体   中英

How to convert Array Object to Object in React native

I called API Called and fetch the array like this.

0:
2019-07-25: {title: "Sub task for 11"}
__proto__: Object
1: {2019-07-19: {…}}
2: {2019-07-24: {…}}
3: {2019-07-26: {…}}
4: {2019-07-25: {…}}
5: {2019-07-24: {…}}
6: {2019-07-25: {…}}
7: {2019-07-25: {…}}

I want convert above object array to the object. Like below..

     "2019-07-25": {title: "Sub task for 11"},
     "2019-07-19": {title: "Sub task for 12"},
     "2019-07-24": {title: "Sub task for 13"},
     "2019-07-26": {title: "Sub task for 14"}

I tried but I can not convert like this. Please anyone know how to convert this help me. Thank you

You can use Object.assign() and spread syntax like this:

 const input = [ { "2019-07-25": { title: "Sub task for 11" } }, { "2019-07-19": { title: "Sub task for 12" } }, { "2019-07-24": { title: "Sub task for 13" } } ]; const output = Object.assign({}, ...input) console.log(output)

You can use reduce to achieve this

 var res = [ { "2019-07-25": { title: "Sub task for 11" } }, { "2019-07-19": { title: "Sub task for 12" } }, { "2019-07-24": { title: "Sub task for 13" } } ].reduce((a, b) => ({ ...a, ...b })) console.log(res)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM