skip to Main Content

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?

2

Answers


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

    Login or Signup to reply.
  2. 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);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search