Home > Mobile >  Need to combine a string and a object into like query params string in JavaScript
Need to combine a string and a object into like query params string in JavaScript

Time:04-15

As the titles says, I have a string and I have a object.

I have to following data

id: 2319213213

Object

{ personName: 'John', personId: 123213213, personGender: 'Male' }

Note: This object I will getting have many properties and all of them can be optional.

Need to combine these and make a string like top in JavaScript/TypeScript

Expected Result

2319213213?personId=123213213&personName=John&personGender=Male

CodePudding user response:

Using URLSearchParams:

const id = 2319213213
    
const person = {
    personName: 'John',
    personId: 123213213,
    personGender: 'Male'
  }

const result = id   '?'    new URLSearchParams(person)
console.log(result)

  • Related