簡體   English   中英

如何在 React Native 中將數組對象轉換為對象

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

我調用 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: {…}}

我想將上面的對象數組轉換為對象。 像下面..

     "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"}

我試過了,但我不能像這樣轉換。 請任何人知道如何轉換這個幫助我。 謝謝

您可以像這樣使用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)

您可以使用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