-2

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

Chanaka
  • 760
  • 1
  • 10
  • 21

2 Answers2

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)
adiga
  • 34,372
  • 9
  • 61
  • 83
1

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)
Nitish Narang
  • 4,124
  • 2
  • 15
  • 22