-1

I'm trying to include BigQuery's query on my application, but I need to split my html(JSON.stringify(response.result.rows, null)) to create a row every row. Is there a way to do it without cycles?

Right now my results are {Result1},{Result2},{Result3}...

I expect something like:

{Result1}
{Result2}
{Result3}
  • What is the original value of `response.result.rows` before you stringify it? – Rory McCrossan Apr 15 '19 at 07:58
  • [The Ask Question Wizard is Live!](https://meta.stackoverflow.com/questions/381671/the-ask-question-wizard-is-live?cb=1) – Ghasem Apr 15 '19 at 07:59
  • @AlexJolig the Question Wizard doesn't match perfectly with with my question...I tried follow it as much as I could – Mattia Puntarello Apr 15 '19 at 08:03
  • @RoryMcCrossan it comes from the DB query, it is something like `{"f":[{"v":"Ciao Enrico!"}]},{"f":[{"v":"Come posso aiutarti?"}]},{"f":[{"v":"Ciao Cesare! Buon anno!"}]}` – Mattia Puntarello Apr 15 '19 at 08:05
  • You can ask in meta about how to fit my question in your wizard system. Help the community to fix the wizard problems – Ghasem Apr 15 '19 at 08:25

2 Answers2

0
"{Result1},{Result2},{Result3}".replace(/},{/g,"}\n{") 

The best practice

evilGenius
  • 1,041
  • 1
  • 7
  • 16
  • Uncaught TypeError: "{Result1},{Result2},{Result3}".Replace is not a function – Lajos Arpad Apr 15 '19 at 08:10
  • Try with `.replace` – freedomn-m Apr 15 '19 at 08:16
  • just use lower case) – evilGenius Apr 15 '19 at 08:22
  • I still have some problems, 'cause the string contains other elements and its like: `[{"f":[{"v":"Ciao Enrico!"}]},{"f":[{"v":"Come posso aiutarti?"}]}, ... ]` – Mattia Puntarello Apr 15 '19 at 08:25
  • @evilGenius edit your answer then. Note that Javascript replace replaces the first occurrence in the way you call it. If you want a replace all, then you will need to use regular expressions: https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript – Lajos Arpad Apr 15 '19 at 08:28
  • but it's still have to work for you. cuz we check by },{ , not just , – evilGenius Apr 15 '19 at 08:30
  • @evilGenius no offence, but no. You should try your own solution before posting. "{Result1},{Result2},{Result3}".replace("},{","}\n{") replaces only the first occurrence of },{ to }\n{ – Lajos Arpad Apr 15 '19 at 09:07
0

Simple: split your string by },{ and join by }\n{:

html(JSON.stringify(response.result.rows, null).split("},{").join("}\n{"))
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175