skip to Main Content

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(/(rn|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?

2

Answers


  1. 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>`);
            });
        }
    });
    
    Login or Signup to reply.
  2. 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(/(rn|n|r)/gm, '')
        description = description.replace(/^/, '* ')
        text.push(description)
    }
    
    text = text.toString()
    text = text.split('*')
    text = text.join('n * ')
    
    console.log(text);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search