Home > Blockchain >  JavaScript: Convert array to object without key or string
JavaScript: Convert array to object without key or string

Time:10-12

I have an array like [1,2,3,4,5]. I want to convert it to object like {1,2,3,4,5} or 1,2,3,4,5.

I would be thankful if someone helps me.

CodePudding user response:

for 1,2,3,4,5, you just have to use .join()

const arr = [1,2,3,4,5]
console.log(arr.join())

Also, specifically for query parameters, in browser or Node, you could use URLSearchParams, which helps you define your query object

const obj = { a: [1,2,3,4,5] }
const searchParams = new URLSearchParams(obj).toString();

console.log(searchParams)

  • Related