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

362
Vistas
JS - How can i compare two item in an array with nearly similar string

Example 1: I have an array like this

let arrayA = ["task A - memberA", "task A - memberB", "task B - memberA"]

I want to find out if two members doing the same task, then remove the task from the after one. The expected output of Example 1 should be:

arrayA = ["task A - memberA", "task B - memberA"]

Example 2:

let arrayA = [{task: "Task A", member: "MemberA"},{task: "Task A", member: "MemberB"},{task: "Task B", member: "MemberB"}]

Expected result 2:

arrayA = [{task: "Task A", member: "MemberA"},{task: "Task B", member: "MemberB"}]

Thank you,

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Create a Set that keeps track of active tasks.

Then loop over the tasks array and extract the task by splitting the string and then if the task is not present in the Set then push it to the resultant array.

let tasks = ["task A - memberA", "task A - memberB", "task B - memberA"];

function getUniqueTasks(tasks) {
  const activeTasks = new Set();
  const uniqueTasks = [];
  tasks.forEach((t) => {
    const [task] = t.split(" - ");
    if (!activeTasks.has(task)) {
      uniqueTasks.push(t);
      activeTasks.add(task);
    }
  });
  return uniqueTasks;
}

console.log(getUniqueTasks(tasks));

If you're looking for a fancy one liner, then refer to the solution below:

const 
  data = ["task A - memberA", "task A - memberB", "task B - memberA"],
  filterer = (s) => (d, _i, _a, t = d.split(" - ")[0]) => !s.has(t) && s.add(t),
  result = data.filter(filterer(new Set()));

console.log(result);

Relevant documentations:

  • Set - JavaScript
  • Array.prototype.forEach
  • Array.prototype.filter
  • String.prototype.split
about 4 years ago · Juan Pablo Isaza Denunciar

0

by using reduce method,

  • get substring before '-' i.e task name
  • check whether task name exist in pv or not
  • if pv (previousValue) array of reduce function, doesn't contain the task then add that new task into it.

hint:
some method returns true or false
includes method return true or false
substr method return sub string from string i.e task name
refer MDN tutorials for more details

let arr = ["task A - memberA", "task A - memberB", "task B - memberA"]

let result = arr.reduce((pv,cv) => {
  if(!pv.some(e => e.includes(cv.substr(0, cv.indexOf('-')).trim()))) pv.push(cv)
  return pv
},[])

console.log(result)

for second example just replace
if(!pv.some(e => e.task.includes(cv.task.trim()))) pv.push(cv)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could take a Set and a function for wanted unique part of the string.

const
    getTask = s => s.split(' - ', 1)[0],
    uniqueBy = fn => s => v => (w => !s.has(w) && s.add(w))(fn(v)),
    data = ["task A - memberA", "task A - memberB", "task B - memberA"],
    result = data.filter(uniqueBy(getTask)(new Set));
    
console.log(result);

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