Как отформатировать большую переменную текста в меньшие переменные в JavaScript? - PullRequest
0 голосов
/ 29 марта 2019

Я использую API для передачи данных в свое приложение, и я вытягиваю объект и его свойство, однако свойство представляет собой просто большую стенку текста (показано ниже). Причина, по которой я не могу использовать весь текст, хорошо ... Потому что n

Можно ли отформатировать эту стенку текста в меньшие переменные или что-то подобное?

Мой текущий код находит длину от начала текста до первого полного останова, а затем создает переменную с такой длиной, чтобы захватить первое предложение.

//what it currently shows//
"Leonard Shelby is tracking down the man who raped and murdered his wife. 
The difficulty of locating his wife's killer, however, is compounded by 
the fact that he suffers from a rare, untreatable form of short-term 
memory loss. Although he can recall details of life before his accident, 
Leonard cannot remember what happened fifteen minutes ago, where he's 
going, or why."

//What I want it to look like
"Leonard Shelby is tracking down the man who raped and murdered his wife.

The difficulty of locating his wife's killer, however, is compounded by 
the fact that he suffers from a rare, untreatable form of short-term 
memory loss.

Although he can recall details of life before his accident, 
Leonard cannot remember what happened fifteen minutes ago, where he's 
going, or why."


//my code//
var fullTvShowOverview = tvShow.overview;
var tvShowOverView = document.getElementById("tvshow-description");

var trimmedTvShowLength = fullTvShowOverview.indexOf(".");
var trimmedTvShowOverview = fullTvShowOverview.substring(0, 
trimmedTvShowLength,  ".");
trimmedTvShowOverview = trimmedTvShowOverview + ".";
tvShowOverView.textContent = trimmedTvShowOverview;


//my code produces: "Leonard Shelby is tracking down the man who raped and murdered his wife."

Ответы [ 2 ]

0 голосов
/ 29 марта 2019

вы можете поместить в массив и просто использовать индексы для доступа к каждому предложению

var fullTvShowOverview = "Leonard Shelby is tracking down the man who raped and murdered his wife.The difficulty of locating his wife's killer, however, is compounded by the fact that he suffers from a rare, untreatable form of short-term memory loss.Although he can recall details of life before his accident,Leonard cannot remember what happened fifteen minutes ago, where he's going, or why.";
var tvShowOverView = document.getElementById("tvshow-description");

var arraySplitByPeriod = fullTvShowOverview.split(".")

tvShowOverView.textContent = arraySplitByPeriod[0];
<div id="tvshow-description">

<div>
0 голосов
/ 29 марта 2019

Вы захотите разбить текстовую строку на массив, а затем зациклить массив для дальнейшей обработки.Вот минимальный пример, который разбивается на .

let textStr ="This. is some. example. text.";

let results = textStr.split(". ");

console.log( results[1] ) 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...