【发布时间】:2019-10-27 13:17:05
【问题描述】:
当我在 textarea 中写一些段落然后提交它时,它会转到后端,整个段落被分成几行。我想将那些拆分数组的行号存储到一个数组中,我该怎么做?
api 会这样发回数据
{
"sentence": [
"Australia’s unexpected exit from the world cup has left all of us dismal and dejected.",
"As we desperately search for that ray of sunlight amidst all this doom and gloom, even the weather seems to have forsaken us.",
"The skies have darkened, as though in response to our collective anguish and the next weekend seems a million years away.",
]
}
您可能想知道我为什么要这样做。这是因为我还将从 api 获取行号,并且我必须将特定的行号与从 api 获取的行号匹配,然后替换该特定行中的某个单词。
这是怎么做到的?我很新,所以请原谅天真的错误
const response = await fetch(url, options);
const result = await response.json();
const {
lineNumber
} = result.sentence[];
更新:
在第 2 行,我必须显示一个下拉菜单,并在点击时将“集体”一词替换为“内部”或“恒定”。
所以我的网站所做的是它会替换几个词,情绪的分数会改变。将其视为语法,它们用于根据语法替换单词,这里是情感。我基本上有一个文本区域,用户将在其中输入一些文本,然后它会转到 api。然后我将 textarea 段落拆分为数组,每个行号将与一个行号相关联。 api 还将发送需要更改情绪的行号。所以现在我们检查段落中的任何行号是否与从 api 发送的任何行号匹配,在我们的例子中,它显示第 2 行,需要替换一个单词。
我想要的只是检查'sentence'数组中的行号与推荐json对象中的行号,然后在推荐json对象中的单词下方显示下划线,这里将是单词'集体'。 我希望我很清楚
"recommendations": {
"anger": {
"2": {
"collective": [
"inner",
"constant"
]
}
}
【问题讨论】:
-
您实际上不必这样做。因为“行”已经在数组中,所以数组索引自然用作行号。
-
如何使用 api 中的行号检查句子中的行号。我已经用 api 发送的行号更新了问题。
标签: javascript reactjs api