Home > OS >  how to add comma after each object and convert into array in javascript react js
how to add comma after each object and convert into array in javascript react js

Time:08-08

how can i add comma after each object and convert into array. here's my response data

{"url":"example.com/john","age":"32"}{"url":"example.com/mike","age":"42"}

i want this

[{"url":"example.com/john","age":"32"},{"url":"example.com/mike","age":"42"}]

i tried with regex but it's converting data into string but i want to render this data

CodePudding user response:

How about a little bit of Regex magic to make it valid JSON.

const res = '{"url":"example.com/john","age":"32"}{"url":"example.com/mike","age":"42"}';

const betterJson = `[${res.replace(/}{/g,'},{')}]`;
console.log(JSON.parse(betterJson))

  • Related