L
L
Lorem Ipsum2021-06-25 20:29:46
JavaScript
Lorem Ipsum, 2021-06-25 20:29:46

How to convert object to URI?

What to get:

https://somesite.com/crm.contact.add.json?fields%5BNAME%5D=LOREM&fields%5BSECOND_NAME%5D=IPSUM&fields%5BLAST_NAME%5D=DOLOR


What is:
const OBJECT = {
  fields: {
    NAME: 'LOREM',
    SECOND_NAME: 'IPSUM',
    LAST_NAME: 'DOLOR',
  }
};


There is a similar analog in PHP:
http_build_query();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-06-25
@bingo347

https://developer.mozilla.org/ru/docs/Web/API/URLS...
https://developer.mozilla.org/ru/docs/Web/API/URL/URL

const OBJECT = {
  fields: {
    NAME: 'LOREM',
    SECOND_NAME: 'IPSUM',
    LAST_NAME: 'DOLOR',
  }
};
const url = new URL('https://somesite.com/crm.contact.add.json');
for (const [name, value] of Object.entries(OBJECT.fields)) {
    url.searchParams.append(`fields[${name}]`, value);
}
console.log(url.href);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question