Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda