简体   繁体   English

如何在 React Native 中将数组对象转换为对象

[英]How to convert Array Object to Object in React native

I called API Called and fetch the array like this.我调用 API Called 并像这样获取数组。

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:您可以像这样使用Object.assign()传播语法

 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您可以使用reduce来实现此目的

 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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