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

179
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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