0

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
                   ]
]

2 Answers 2

1

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",
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

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 ] ])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.