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

184
Views
Filter duplicate values in string Javascript

When mentioning users, if I mention one user more than once, I get string like:

"@[Firstname Lastname](4652) @[Firstname Lastname](4652) @[Firstname Lastname](4652)"

I tried to filter so the payload contains only one of those values. (@[Firstname Lastname](4652)) The first problem I occured is the space is optional, so I couldn't split the array (I created an array out of string) by space. If I split it with @, it doesn't send the @ in the payload. I assume I should create some kind of regex which will compare text between '@' and last ')', based on uses's ID which is in the brackets. But I was unable to create one. Another issue is that users can write text as well, not just mention users, so it could look like:

"bla bla @[Firstname Lastname](4652) bla bla @[Firstname Lastname](4652) bla bla"
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can first split the entire string using split(" ")

const string = "bla bla @[Firstname Lastname](4652) bla bla @[Firstname 
Lastname](4652) bla bla";

const stringArr = string.split(" "); //Split the string into array of strings

let users = [];
const userPattern = /@[a-z]*)/i; //Begins with `@` and ends with `)`    

stringArr.forEach((item) => {
   if(userPattern.test(item)
   {
      users.append(item);
   }
});

users = unique(users);

Function to remove duplicates

function unique(a) {
    var seen = {};
    var out = [];
    var len = a.length;
    var j = 0;
    for(var i = 0; i < len; i++) {
         var item = a[i];
         if(seen[item] !== 1) {
               seen[item] = 1;
               out[j++] = item; //Adding unique arrays to this array
         }
    }
    return out;
}

Hope this helps 😊

about 4 years ago · Santiago Trujillo Report

0

I think "@" is the safest way to split the string because that ensures only one user id is present in each element. Since "@" is always followed by "[", it can be added back using:

dataPart.replace("[", "@[");

That issue resolved, it becomes a case of keeping track of id numbers as you add them to a new 'payload' array or string.

In the implementation suggested in my snippet, the data is split at "@" into an array. The array is then looped though using forEach. Each element is examined for the presence of an Id number - located by the presence of "(". New Ids are added to a seen array which can be checked during subsequent loops to see if the id has been seen before. If the Id is unique, the element is transferred to the payload (and the id added to seen).

The limitations are that the id must be the only part enclosed in brackets "()", and if there is extra text in subsequent repeats of a given id, it will be lost (but text at the beginning will persist.

let atList = "hello @[Firstname Lastname](4652) @[Firstname Lastname](4652)   @[Firstname Lastname](2352) @[Firstname Lastname](4652) @[Firstname Lastname](1637)";

const parts = atList.split("@");
const payload = [];
const seen = [];

 parts.forEach(element => {
 
   currentId = element.slice(element.indexOf("(")+1,element.indexOf(")"));
   
   if (seen.indexOf(currentId) == -1 && element.length > 0) {
   
     payload.push(element.replace("[", "@["));
     seen.push(currentId);
   } 
   
 }); 
 
 payloadString = payload.join("");
 console.log(payloadString);

about 4 years ago · Santiago Trujillo Report

0

First, just split the string into an array at the end of each username, which is the ) character. Remove all characters before the @ symbol, then add the new array element to a second array if it is not already present.

EDIT: Made code executable

function getUsernames(string) {
  var array = [] // Second Empty Array
  for (var s of string.split(")")) { // For Loop through first 'split' array
        if (!array.includes(s.substring(s.indexOf("@"))+")") && s.includes("@")) array.push(s.substring(s.indexOf("@"))+")") // If the second array doesn't have the split element, add it
  }
  return(array)
}
console.log(getUsernames("@[Firstname Lastname](4652) @[Firstname Lastname](4652) @[Firstname Lastname](4652)")) // Print out your refined array of usernames
console.log(getUsernames("@[Firstname Lastname](0001) abcd @[Firstname Lastname](0002) 1234 @[Firstname Lastname](0003) <?/[}")) // Print out your refined array of usernames

about 4 years ago · Santiago Trujillo 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!