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

468
Vistas
Javascript:Delete duplicate in 2D Array

I have a 2D array like this one in JavaScript:

result=[["user1", "java"], ["user2", "python"],["user1", "java"], ["user1", "C++"], ["user1", "java"], ["user2", "Python"]....]  

you can see that ["user1", "java"] comes 3 times and ["user2", "python"] 2 times.

I want to clean this array so that every couple of element must appear only once.it means that if "user1" can appear again, it should be with another language but not with "java". can some one help me?

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

0

You could try this one:

let result= [
  ["user1", "java"], 
  ["user2", "python"],
  ["user1", "java"], 
  ["user1", "C++"], 
  ["user1", "java"], 
  ["user2", "Python"]
];
const map = new Object();
result.forEach((item) => {
  if(!map[item[0]]){
    map[item[0]] = new Array();
  }
  const user = map[item[0]];
  const language = (item[1] || '').toLowerCase().trim();
  if(user.indexOf(language) < 0){
    user.push(language);
  }
});
result = new Array();
Object.keys(map).forEach((user) => {
  map[user].forEach((language) => {
    result.push([user,language]);
  });  
});
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can create a Set out of the array by joining the user and language strings, which would remove the duplicates. And then simply split the strings back to get the desired array.

const 
  result = [["user1", "java"], ["user2", "python"], ["user1", "java"], ["user1", "c++"], ["user1", "java"], ["user2", "python"]],
  uniques = Array.from(new Set(result.map((res) => res.join("-")))).map(
    (data) => data.split("-")
  );

console.log(uniques);

Relevant Documentations:

  • Array.from
  • Set
  • Array.prototype.map
  • Array.prototype.join
  • String.prototype.split
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do this with a single loop over the array using reduce, join and Set as:

const result = [
    ['user1', 'java'],
    ['user2', 'python'],
    ['user1', 'java'],
    ['user1', 'C++'],
    ['user1', 'java'],
    ['user2', 'python'],
];

const [uniqueElements] = result.reduce((acc, curr) => {
        const [uniq, set] = acc;
        if (!set.has(curr.join(','))) {
            set.add(curr.join(','));
            uniq.push(curr);
        }
        return acc;
    },
    [[], new Set()],
);

console.log(uniqueElements);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

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