0

I am new to JavaScript. I am building an array of objects. I need to stringify it, get rid of the square brackets, and split each object on ',' and put on new line.

Need to go from:

[{"product_id":297316,"id":1,"rating":3},{"product_id":98133,"id":2,"rating":2}]

To this:

"{"product_id":297316,"id":1,"rating":3}",
"{"product_id":98133,"id":2,"rating":2}"

2 Answers2

3

If by put on a new line you mean print, you can do:

for (let product of products) {
  console.log(JSON.stringify(product))
}

Or if you want to produce a newline-separated string you can do:

products.map(JSON.stringify).join('\n')
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
0

So.... I got that you need understand code lines for testing or what information comes from API or something like that.

Most of the time I use replace using regex in Sublime Text 3 in a fast way, like:

Example

Chris Sum
  • 123
  • 1
  • 2
  • 11
  • If you use Google Chrome could use a plugin called JSONView to parse automatically json info. If you want get to a major league you can use Postman. – Chris Sum Oct 17 '20 at 04:15