Mahesh Uday
Mahesh Uday

Reputation: 7

Convert the following array data as Json in react

I need to convert the following array data to Json in react. I tried map method, but it is not the correct way. I need key value pair, so that i can pass it to server as json

 [
                   [
                      "channel",
                      "s1"
                   ],
                   [
                      "category",
                      "Account 1"
                   ],
                   [
                      "accountAdministration",
                      "Partnership 1"
                   ],
                   [
                      "partnershipAccounting",
                      "1 level Performance issues"
                   ],
                   [
                      "requestCategory",
                      "Research"
                   ],
                   [
                      "severity",
                      "Blocker"
                   ],
                   [
                      "activationDate",
                      "2020-10-29T05:54:00.000Z"
                   ],
                   [
                      "managerApproved",
                      true
                   ]
]

Upvotes: 0

Views: 69

Answers (2)

Mahesh Uday
Mahesh Uday

Reputation: 7

Worked with

Object.fromEntries([ [ "channel", "s1" ], [ "category", "Account 1" ], [ "accountAdministration", "Partnership 1" ], [ "partnershipAccounting", "1 level Performance issues" ], [ "requestCategory", "Research" ], [ "severity", "Blocker" ], [ "activationDate", "2020-10-29T05:54:00.000Z" ], [ "managerApproved", true ] ])

Upvotes: 0

Idrizi.A
Idrizi.A

Reputation: 12060

Try using reduce and creating an object:

var arr = []; // your array

var data = arr.reduce((map, item) => {
  map[item[0]] = item[1];
  return map;
}, {});

The data object will be in the following format:

{
  "accountAdministration": "Partnership 1",
  "activationDate": "2020-10-29T05:54:00.000Z",
  "category": "Account 1",
  "channel": "s1",
  ...
}

Upvotes: 1

Related Questions