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

92
Vistas
How to check if the last characters in an array meet a condition

I am trying to remove the last characters of an element in an array only if they meet a condition. For example: if they end in s, then I remove the s. If not, the element should remain the same. This is how I'm trying to do it:

let myList = []
for (let i = 0; i < arrays.length; i++){
        if (arrays[i].substring(arrays[i].length - 1) == 's'){
        item = arrays[i].slice(0,-1);
        myList.push(item);
        }else{
            myList.push(arrays[i]);
        }

It's not working though, and idea why?

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

0

this way

const 
  arr    = ['Johns','Paul', 'ringos', 'Georges']
, myList = arr.map( name => /s$/.test(name) ? name.slice(0, -1) : name)
  ;

console.log( myList )

about 4 years ago · Juan Pablo Isaza Denunciar

0

If the name endsWith s then you can slice it as:

const arr = ["Johns", "Paul", "ringos", "Georges"];
const myList = arr.map(name => name.endsWith("s") ? name.slice(0, -1) : name);
console.log(myList);

about 4 years ago · Juan Pablo Isaza Denunciar

0

From the code you have write, it seems it lacks }. And if you wonder why nothing happen in the terminal, it's because you have not write console.log(myList) in your code. This code should works:

const arrays = ['Apples', 'Oranges', 'Bananas', 'Pear']
let myList = []
for (let i = 0; i < arrays.length; i++){
        if (arrays[i].substring(arrays[i].length - 1) == 's') {
        item = arrays[i].slice(0,-1);
        myList.push(item);
        } else {
            myList.push(arrays[i]);
        }
    }

console.log("List:", myList)

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