【发布时间】:2021-05-26 03:27:45
【问题描述】:
我想通过使用 for 循环函数来获取数组 split_Sentence 中所有出现的 "they" 和 "some" 的位置。然后我想从 for 循环函数的输出构建一个数组。但问题是,每当我使用indexOf() 查找位置时,它总是给出第一次出现的位置。我不知道如何从 for 循环的输出中创建一个数组。
谁能帮助我,我将不胜感激。
var Sentence = "some warriors know when they are defeated, whether some of them are too stubborn to accept it and they are the one who die. "
var split_Sentence = Sentence.split(" ")
/* function() ==> then sort an array listing all its output*/
Expected outcome: final array should be like this array= (0,4,8,18)
如果你不明白我的问题,请尽管问我
【问题讨论】:
-
请在您的问题中添加预期结果。(您可以使用正则表达式)
-
为什么要使用
for..loop。请改用matchAll。 -
仅供参考
for..loop不是函数
标签: javascript html arrays sorting