0

I want to filter the first paragraph from the course_description object properties. I need only the first paragraph as highlighted in the screenshot. (a big rectangle)

var description = apiData[i].course_description;
console.log(description);
reshtm += '<div class="col-md-6">\
                <a href="coursesdetails" class="well">\
                    <h1>'+apiData[i].course_name+'</h1>\
                    <p>'+apiData[i].course_description+'</p>\
                </a>\
            </div>';

enter image description here

Screenshot of response enter image description here

Santosh
  • 3,477
  • 5
  • 37
  • 75
  • It would help if you could give the entire api response or the structure. – varun agarwal Feb 18 '19 at 06:22
  • I just want to filter the text inside the `apiData[i].course_description;` why all response is necessary? Please help. – Santosh Feb 18 '19 at 06:25
  • What is the data type of `apiData[i].course_description`? – varun agarwal Feb 18 '19 at 06:27
  • Its a string. the screenshot I post is of console.log. I added screenshot aswelll. Thanks. – Santosh Feb 18 '19 at 06:28
  • There is no JSON in this question. – axiac Feb 18 '19 at 06:31
  • @axiac I have added few minutes back. Please check. I have added the screenshot of json response for that particular object properties. – Santosh Feb 18 '19 at 06:32
  • The question is still not about JSON. What you ask is a simple string processing that can be done in several ways using the [methods of `String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String). – axiac Feb 18 '19 at 06:35

3 Answers3

1

You could try this:

var elem  = document.createElement('DIV');
elem.innerHTML = apiData[i].course_description;
var result = elem.firstChild;
console.log(result);

PS: You might get the escaped characters in the result which you can later trim it off.

varun agarwal
  • 1,491
  • 6
  • 9
1

As your string format (SS) , you can do something like below

var description = apiData[i].course_description.split("\r")[0]; // get first para
console.log(description);
0

You can use truncate function like here

Abbas Jaber
  • 39
  • 2
  • 9