簡體   English   中英

如何將行號存儲在數組中?

[英]How do I store the line numbers in an array?

當我在 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 行,我將不得不顯示一個下拉菜單,並在點擊時將“集體”一詞替換為“內部”或“常量”。

所以我的網站所做的是它會替換幾個詞,情緒的分數會改變。 將其視為語法,它們用於根據語法替換單詞,這里是情感。 我基本上有一個文本區域,用戶將在其中輸入一些文本,它將 go 到 api。 然后我將 textarea 段落拆分為數組,每個行號將與一個行號相關聯。 api 還會發送需要更改情緒的行號。 所以現在我們檢查我們段落中的任何行號是否與從 api 發送的任何行號匹配,在我們的例子中,它顯示第 2 行,需要替換一個單詞。

all i want is to check the line number from the 'sentence' array with the line number from the recommendation json object and then show an underline under the word that is in the recommendation json object, here it will be the word 'collective'. 我希望我很清楚

"recommendations": {
    "anger": {
      "2": {
        "collective": [
          "inner",
          "constant"
        ]
      }
    }

您可以使用map()將結果轉換為具有鍵/值對的對象數組:

 var result = { "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.", ] } result = result.sentence.map((s, idx) =>{ return {text: s, lineNumber: (idx+1)} }); console.log(result);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM