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

220
Vistas
How to split combined string into multiple strings in array?

How can I split this array with commas? I have multiple arrays

[ "LeadershipPress" ]
[ "BusinessLeaderLeadershipPress" ]
[ "LeaderLeadershipPoliticsPress" ]
etc.

scraper.scrapeListingPages('article.article', (item) => { 
    var categories = $(item).find('a[rel = "category tag"]').text().split();
    console.log(categories);
    categories.forEach(function(i){
       $(i).find('a[rel = "category tag"]')
       console.log(i);
    })
});

Right now my output in the console is

Array [ "BusinessLeaderLeadershipPress" ]
BusinessLeaderLeadershipPress

I want to split the categories into an array with commas without having to use separator, limits or regex because I have multiple random arrays.

Is there a way I can use a forEach or for loop to accomplish this?

The result I want is [ "Business, Leader, Leadership, Press" ]

Thanks

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

without having to use separator, limits or regex because I have multiple random arrays.

A simple approach using for-loop with algorithm taking Consideration that string starts with Uppercase: Looping through characters of the string if char is not UPPERCASE accumelate chars to variable word, until encounter Uppercase letter, then push it in res Array.

const string = "BusinessLeaderLeadershipPress";
let i = 1;
let character = "";
let word = string[0];
const res = [];
while (i <= string.length) {
  character = string.charAt(i);
  if (character == character.toUpperCase()) {
    res.push(word);
    word = character;
  } else {
    word += character;
  }
  i++;
}
console.log(res); //['Business', 'Leader', 'Leadership', 'Press']
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could split a string with a look ahead for an uppercase letter.

const
    string = 'BusinessLeaderLeadershipPress',
    result = string.split(/(?=[A-Z])/);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar
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