1

I have this code which grabs all the input type from a form

  const form = document.querySelector('form');
  const data = new URLSearchParams(new FormData(form).entries());

My question is how to JSON.stringify the above data object? I need to stringify so that I can pass it through JQuery ajax to post the data.

If I loop it, it will print out below

for (let p of data) {
  console.log(p);
}

enter image description here

1
  • Can you post a sample of data structure if you console.log(data);? Commented Nov 26, 2020 at 4:17

1 Answer 1

4

Does this achieve what you are looking for? The data is an array of arrays because Object.entries has been called on it. The Object.fromEntries() method transforms a list of key-value pairs into an object. After it's back to being an object, call JSON.stringify().

const obj = Object.fromEntries(data);
const stringifyObj = JSON.stringify(obj);
Sign up to request clarification or add additional context in comments.

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.