Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
Function that returns all indexes that do not overlap - Javascript

I have an array like so:

[
    {
        indexes: [1, 3, 5, 8],
        length: 2
    }, 
    {
        indexes: [2, 4, 6, 7],
        length: 1
    }
]

I am using this to split a string (and many other similar ones):

matatatatt

I am trying to split it by the array ['at', 't'], which should return something like this in the future:

['m', 'at', 'at', 'at', 'at', 't']

This is the code I currently have:

function splitVars (eq, vars) {
        let splitted = eq.split("");
        let locs = [];

        for (let i = 0; i < vars.length; i++) {
            let variable = vars[i];

            let found = locations(variable, eq)

            locs.push({
                indexes: found,
                variable: variable,
                varLength: variable.length
            })
        }

        return locs;
    }

    function locations(substring, string){
        var a=[],
            i=-1;
        while((i = string.indexOf(substring, i + 1)) >= 0) a.push(i);
        return a;
    }        

console.log(splitVars('wmtmtmtt', ['mt', 't']));

However, back to my question. As you can tell, because the length of the second string is one, it is picking up the one letter in the first indexes that is the same. Because the indexes [2, 4, 6] are overlapping with the first indexes, I would like to remove them.

How can I do such a thing?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda