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

321
Vistas
How to slice the name in the array of objects discluding the names without (T1) or (T2)

I'm having Problem with slicing an array of objects

const agent = [
  {
    "agent-name": "Jm Fajardo",
    "gender": "Male"
  },
  {
    "agent-name": "Lem Cruz (T1)",
    "gender": "Male"
  },
  {
    "agent-name": "Levi Fajarda (T2)",
    "gender": "Male"
  },
  {
    "agent-name": "Ian Pacido",
    "gender": "Male"
  }
];


$.each(agent, function (key, value) {
  var sliced = value["agent-name"].slice(0, -5);
  console.log("agent :", sliced);
});

This is the output I get:

agent : Jm Fa
agent : Lem Cruz
agent : Levi Fajarda
agent : Ian P

This is the output i tried to get

agent : Jm Fajardo
agent : Lem Cruz
agent : Levi Fajarda
agent : Ian Pacido
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use a regular expression to only match patterns like (T1) or (T2).

const agent = [{
    "agent-name": "Jm Fajardo",
    "gender": "Male"
  },
  {
    "agent-name": "Lem Cruz (T1)",
    "gender": "Male"
  },
  {
    "agent-name": "Levi Fajarda (T2)",
    "gender": "Male"
  },
  {
    "agent-name": "Ian Pacido",
    "gender": "Male"
  }
];


$.each(agent, function(key, value) {
  var sliced = value["agent-name"].replace(/\s*\(T\d\)$/, '');
  console.log("agent :", sliced);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Explanation:

  • \s*: all spaces before the pattern
  • \(T\d\): match all (Tx) where x is a digit
  • $: at the end of the string

$ can be omitted to match (Tx) anywhere in the string.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you would like to avoid regular expressions you could try testing for the presence of T1 or T2 and then only applying your slice function if one of them is present.

I think using regexp would probably be a more generally applicable approach.

const agent = [{
    "agent-name": "Jm Fajardo",
    "gender": "Male"
  },
  {
    "agent-name": "Lem Cruz (T1)",
    "gender": "Male"
  },
  {
    "agent-name": "Levi Fajarda (T2)",
    "gender": "Male"
  },
  {
    "agent-name": "Ian Pacido",
    "gender": "Male"
  }
];


$.each(agent, function(key, value) {
  if(value["agent-name"].includes(['T1']) || value["agent-name"].includes(['T2'])){
    var sliced = value["agent-name"].slice(0, -5);
  } else {
    var sliced = value["agent-name"];
  }
  
  console.log("agent :", sliced);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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