javascript - How to convert Array Object to Object in React native - Stack Overflow

I called API Called and fetch the array like this.0:2019-07-25: {title: "Sub task for 11"}_

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

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

Share Improve this question edited Jul 13, 2019 at 5:56 Chanaka asked Jul 13, 2019 at 5:48 ChanakaChanaka 7781 gold badge11 silver badges22 bronze badges 2
  • 2 please post the code also you have tried so far – Code Maniac Commented Jul 13, 2019 at 5:58
  • 1 that's a javascript issue – Rodrigo Commented Jul 13, 2019 at 6:32
Add a ment  | 

2 Answers 2

Reset to default 5

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745220217a4617214.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信