-1

I am using ajax to send FormData and receiving the output in the following format

Result=A&Status=Approved&Error=&ErrorCode=00000

I want to convert the same into a JS object (key value pair) e.g.

{Result: "A", Status: "Approved", Error: "", ErrorCode: "00000"}

I can do that using string split and other string functions or regex but since the output format is consistent i was wondering if this is a known data type and if yes is there a way to convert it using native functions?

0

2 Answers 2

1

You can use Object.fromEntries in conjunction with the URLSearchParams constructor.

const obj = Object.fromEntries(new URLSearchParams(
    'Result=A&Status=Approved&Error=&ErrorCode=00000'));
console.log(obj);

Sign up to request clarification or add additional context in comments.

1 Comment

Since the data was not retrieved from the url, I never looked at it like GET parameters. URLSearchParams works as intended.
-1

Take a look at new URLSearchParams(), it should be a way better solution than string manipulation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.