Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda