Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

314
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!