-1

I get a text below this from the database which has multiple strings with double quotes like the one below,

["Time Usage: 9.00am - 4.00pm", "Rental of Commune Room 01 (7 Hours)", "55" Smart TV, 1x Clear Writing Glass, Marker Pen, HDMI Cable, Complimentary WiFi, and Filtered Water"]

Expect Output to Customer

  • Time Usage: 9.00am - 4.00pm
  • Rental of Commune Room 01 (7 Hours)
  • 55" Smart TV, 1x Clear Writing Glass, Marker Pen, HDMI Cable, Complimentary WiFi and Filtered Water

Current Output I get from below code

  • Time Usage: 9.00am - 4.00pm,Rental of Commune Room 01 (7 Hours),55" Smart TV
  • 1x Clear Writing Glass
  • Marker Pen
  • HDMI Cable
  • Complimentary WiFi
  • and Filtered Water

I used the below code,

let description = (["Time Usage: 9.00am - 4.00pm",
"Rental of Commune Room 01 (7 Hours)",
"55\" Smart TV, 1x Clear Writing Glass, Marker Pen, HDMI Cable, Complimentary WiFi, and Filtered Water"]
)

    description = description.toString()
    description = description.replace(/(\r\n|\n|\r)/gm, '')

    if (description !== '') {
      description = description.replace(/^/, '* ')
      description = description.replace(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/gm, '|')
    }

    description = description.split('|')
    description = description.join('\n * ')

console.log(description);

Can someone helps me to fixed this?

  • Why not a `description[2]` – User863 Jul 26 '23 at 10:30
  • @User863 description[2] mean? – Malith Fernando Jul 26 '23 at 10:32
  • Try it in the `console.log()` – User863 Jul 26 '23 at 10:33
  • @User863 just print "T" – Malith Fernando Jul 26 '23 at 10:34
  • Try it before converting to string using `toString()` – User863 Jul 26 '23 at 10:35
  • Does this answer your question? [How to get value at a specific index of array In JavaScript?](https://stackoverflow.com/questions/8238456/how-to-get-value-at-a-specific-index-of-array-in-javascript) – User863 Jul 26 '23 at 10:37
  • @User863 I'm trying to print every text in an array as a bullet point. So ["abc", "abc, abd", "dog"] need to print like this * abc * abc, abd * dog. Not trying to get a specific index. – Malith Fernando Jul 26 '23 at 10:38
  • @User863 please check my expected output and current output to see the difference! – Malith Fernando Jul 26 '23 at 10:40
  • 3
    _"like the one below"_ - are you really getting _exactly_ that? If the double quote after the `55` was properly escaped using a backslash, the whole thing would be perfectly fine JSON, which you could simply _decode_ to get an actually array then. – CBroe Jul 26 '23 at 10:43
  • 2
    _"I used the below code"_ - but there you _have_ an array with the three strings as separate elements already, so the "input data" isn't even in the string format you said it was ...? Your question makes little sense, with _that_ code example. – CBroe Jul 26 '23 at 10:46
  • 2
    _"So ["abc", "abc, abd", "dog"] need to print like this * abc * abc, abd * dog."_ - if you are not even trying to create a proper _HTML_ list here, but basically just "ASCII art", you can have _that_ result with really way less code - `"* " + ["abc", "abc, abd", "dog"].join("\n* ")` – CBroe Jul 26 '23 at 10:49

2 Answers2

2

You don't need that fancy code, because description is array (if it's string, do let description = JSON.parse(description)):

let ajaxPlainResponse = "[\"Time Usage: 9.00am - 4.00pm\", \"Rental of Commune Room 01 (7 Hours)\", \"55\\\" Smart TV, 1x Clear Writing Glass, Marker Pen, HDMI Cable, Complimentary WiFi, and Filtered Water\"]";

let description = JSON.parse(ajaxPlainResponse);

console.log(description[0]); console.log(description[1]); console.log(description[2]);

let list = document.getElementById('list');

for (let i in description) {
    let li = document.createElement('li');
    li.appendChild(document.createTextNode(description[i]));
    list.appendChild(li);
}
<ul id="list"></ul>

Please note that with jQuery ajax it would be much faster/less code:

$.ajax({
    url: 'example.com',
    type: 'POST',
    dataType: 'json',
    success: function (response) {
        let list = $('#list');

        $.each(response, function (i, text) {
            list.append(`<li>${text}</li>`);
        });
    }
});
Justinas
  • 41,402
  • 5
  • 66
  • 96
1

You can try something but i prefer above answers.

let descriptions = ["Time Usage: 9.00am - 4.00pm",
"Rental of Commune Room 01 (7 Hours)",
"55\" Smart TV, 1x Clear Writing Glass, Marker Pen, HDMI Cable, Complimentary WiFi, and Filtered Water"]

let text = []

for (let description of descriptions) {
    description = description.toString()
    description = description.replace(/(\r\n|\n|\r)/gm, '')
    description = description.replace(/^/, '* ')
    text.push(description)
}

text = text.toString()
text = text.split('*')
text = text.join('\n * ')

console.log(text);