Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取字符串中没有for或while循环的每个重叠引用的索引_Javascript - Fatal编程技术网

Javascript 获取字符串中没有for或while循环的每个重叠引用的索引

Javascript 获取字符串中没有for或while循环的每个重叠引用的索引,javascript,Javascript,我想得到给定字符串中每个重叠出现的索引 例如: var source = 'NNNHRMSLGGGGEAZZ' var toFind = ['HRMSL', 'NN', 'ZZ', 'GGG'] find(source, toFind) // should print line by line: 'NN' found at index 0 'NN' found at index 1

我想得到给定字符串中每个重叠出现的索引

例如:

var source = 'NNNHRMSLGGGGEAZZ'
var toFind = ['HRMSL', 'NN', 'ZZ', 'GGG']
find(source, toFind) // should print line by line:  'NN' found at index 0
                                                    'NN' found at index 1
                                                    'HRMSL' found at index 2
                                                    'GGG' found at index 8
                                                    'GGG' found at index 9
                                                    'ZZ' found at index 13
如何在不使用
for
而使用
循环(
forEach
很好)的情况下实现这一点

我尝试了多种方法,但都没有考虑到重叠项,这是非常棘手的


谢谢您的帮助。

处理重叠项目的一个简单方法是分别迭代每个
以查找
,在
字符串上,逐个字符迭代,检查在该点切片的草堆是否开始使用
针:

const find=(源代码,toFind)=>{
常量日志=[];
toFind.forEach((find)=>{
[…源].forEach((\ux,索引)=>{
if(source.slice(index.startsWith(find)){
push({index,find}));
}
});
});
排序((a,b)=>a.index-b.index);
logs.forEach({find,index})=>{
log(`${find}在索引${index}`处找到);
});
};
变量源='NNNHRMSLGGGGEAZZ'
var toFind=['HRMSL','AA','ZZ','GGG']

find(source,toFind)
您可以使用对
indexOf()
的递归调用,通过基于先前找到的索引为给定项提供起始索引来查找重叠项。然后,对于数组中的每个项目,您可以
.flatMap()
将每个项目索引对添加到自己的数组中。然后可以对该数组进行排序,然后使用forEach对其进行迭代:

const source='NNNHRMSLGGGGEAZZ';
常数toFind=['HRMSL','NN','ZZ','GGG'];
常量索引=(str,item,i=0)=>{
const idx=str.indexOf(第i项);
返回idx>=0?[[item,idx],…索引(str,item,idx+1)]:[]
}
const getIndexPairs=(源,toFind)=>
toFind.flatMap(item=>index(source,item)).sort(([,a],,b])=>a-b);
getIndexPairs(source,toFind).forEach(([item,index])=>{
console.log(在索引“+索引”处找到项+);

});使用
reduce
创建一个对象,其中key作为arr元素,value作为索引数组,然后迭代对象键并检查其数组长度是否大于1。如果是,则它是一个重复元素

常量arr=[“a”、“v”、“a”]; const groupByDuplicateElements=arr.reduce((a,e,i)=>{ 如果(!a[e]){ a[e]=[]; } a[e]。推(i); 返回a; }, {}); key(groupByDuplicateElements).forEach(key=>{ if(groupByDuplicateElements[key].length>1){ log(`${groupByDuplicateElements[key].join(',')}(${key}')`); }
}); 请说明您尝试了什么以及问题出在哪里。为什么不使用
for
while
循环?它们将是处理这类事情的最佳技术。它们是不必要的,但它们会使代码看起来更好。你能解释一下你最近编辑问题的意图吗?它没有添加任何内容,使您的问题模糊不清。这是一个新问题还是和以前一样的问题?我回到原来的问题上来,让未来的读者更清楚地了解它。如果您有新问题,请将其作为新问题发布。如果要编辑旧问题,请添加到当前问题,而不是删除整个原始问题。@libgo这是一个全新的问题。你最好把它作为一个新的发布。您可以将当前问题回滚到旧问题,然后发布新问题。@libgo请阅读以下内容:以及以下内容: