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

185
Vistas
How to Check All Array Element Inside endswith()

let array1 = ["?", "!", "."];
let array2 = ["live.", "ali!", "harp", "sharp%", "armstrong","yep?"];

console.log(array2.filter((x) => x.endsWith("?")));

The output is just: ['yep?']

Because the function endsWith() only checked for "?" as you see in the code. How do I loop the elements on the array1 (suffixes) inside the endsWith function so the output is:

['live.', 'ali!', 'yep?']
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could use a regex, and then match each element in the filter iteration against it.

/[?!.]$/ says match one of these things in the group ([?!.]) before the string ends ($).

const arr = ['live.', 'ali!', 'harp', 'sharp%', 'armstrong', 'yep?'];
const re = /[?!.]$/;

const out = arr.filter(el => el.match(re));
console.log(out);

Regarding your comment you can pass in a joined array to the RegExp constructor using a template string.

const query = ['.', '?', '!'];
const re = new RegExp(`[${query.join('')}]$`);

const arr = ['live.', 'ali!', 'harp', 'sharp%', 'armstrong', 'yep?'];

const out = arr.filter(el => el.match(re));
console.log(out);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use an inner .some() call to loop over your array1 and return true from that when you find a match instead of hardcoding the ?:

const array1 = ["?", "!", "."];
const array2 = ["live.", "ali!", "harp", "sharp%", "armstrong","yep?"];

const res = array2.filter((x) => array1.some(punc => x.endsWith(punc)));
console.log(res);

Above, when you return true (or a truthy value) from the .some() callback, the .some() method will return true, otherwise it will return false if you never return true, thus discarding it.

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