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

161
Vistas
Split array based on element contents?

Let's say my elements look like this:

const words = [
 'duck foo bar',
 'duck',
 'duck bing ',
 'bing',
 'Bloop#12 goose',
 'duck 12',
 'duck goose',
  ...
]

What I'd like is to split this into chunks where 'goose' is the final element in a chunk:

const words = [
 [
   'duck foo bar',
   'duck',
   'duck bing',
   'bing',
   'Bloop#12 goose',
 ], 
 [
   'duck 12',
   'duck goose',
 ], 
 [
  ...
 ],
];

There's no regularity to how many elements precede a 'goose', nor what is in each element except that 1) goose is always the last part of an element, and 2) goose never appears in any other element besides the one I want a chunk to end on (i.e. I never get 'goose foo', but I might get 'duck goose')

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

0

You could reduce the array and have a look to the previous string and add an array to the result set for a new group.

const
    words = ['duck foo bar', 'duck', 'duck bing ', 'bing', 'Bloop#12 goose', 'duck 12', 'duck goose'],
    separator = 'goose',
    groups = words.reduce((r, s, i, a) => {
        if (!i || a[i - 1].includes(separator)) r.push([]);
        r[r.length - 1].push(s);
        return r;
    }, []);

console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this. This should do what you're looking for.

const words = [
 'duck foo bar',
 'duck',
 'duck bing ',
 'bing',
 'Bloop#12 goose',
 'duck 12',
 'duck goose'
]

const answer = []
let temp = []
for(let i = 0; i < words.length; i++){
  if(words[i].includes('goose')){
    temp.push(words[i])
    answer.push(temp)
    temp = []
  } else{
    temp.push(words[i])
  }
}

console.log(answer)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can achieve this with a simple forEach loop. In my example below I store the values first temporally.

Then I push the chunk into the new array if the word contains goose.

const words = [
 'duck foo bar',
 'duck',
 'duck bing ',
 'bing',
 'Bloop#12 goose',
 'duck 12',
 'duck goose',
 'abc'
]

const n = []
let tmp = [];
words.forEach((w) => {
  if (! w.includes('goose')) {
    tmp.push(w);
  } else {
    tmp.push(w);
    n.push(tmp);
    tmp = [];
  } 
  
});

console.log(n)

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