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

258
Vistas
Remove duplicate certain column values Javascript 2D array

I have an array for example :

var array  = [
    [ 1, "Hello", "red", 0, "yes"],
    [ 2, "Hello", "red", 1, "no"],
    [ 3, "Hello", "blue", 4, "no"],
    [ 4, "Sunshine", "yellow", 5, "yes"],
    [ 5, "Hello", "red", 6, "yes"],.....]

Now I want to remove array based on multiple column lets say (2,3,5): so based on 3 column I want to remove duplicates and keep first occurrence. my result should be like:

array = [[ 1, "Hello", "red", 0, "yes"],
    [ 2, "Hello", "red", 1, "no"],
    [ 3, "Hello", "blue", 4, "no"],
    [ 4, "Sunshine", "yellow", 5, "yes"],....]

you see hello, red & yes matched. in column 2,3,5 so the first occurrence was only kept rest was removed. now I can not figure it out how to solve this complex issue.

function pullgm() {
  // ss = SpreadsheetApp.getActiveSpreadsheet()
  // sh2 = ss.getSheetByName("GP")
  // sh3 = ss.getSheetByName("GM")
  // var lr2 = sh2.getLastRow()
  // var lc2 = sh2.getLastColumn()
  // var lr3 = sh3.getLastRow()
  // var lc3 = sh3.getLastColumn()

  var array = [
    [1, 'Hello', 'red', 0, 'yes'],
    [2, 'Hello', 'red', 1, 'no'],
    [3, 'Hello', 'blue', 4, 'no'],
    [4, 'Sunshine', 'yellow', 5, 'yes'],
    [5, 'Hello', 'red', 6, 'yes'],
  ];

  var hash = Object.create(null),
    length = array.length,
    result = [],
    element,
    i,
    key,
    ref;

  for (i = 0; i < length; i++) {
    element = array[i];
    key = array[i][0] + '|' + array[i][1] + '|' + array[i][6];
    ref = hash[key];
    if (ref) {
      continue;
    }
    hash[key] = element.slice();
    result.push(hash[key]);
  }
  console.log(array);
}
pullgm();

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

0

You could take a Set with a function which builds a key of the wanted indices.

After checking the set with the combined key, add either the key and return the actual data set or discard the actual array.

const
    uniqueBy = (fn, s = new Set) => o => (k => !s.has(k) && s.add(k))(fn(o)),
    key = keys => o => keys.map(k => o[k]).join('|'),
    data = [[1, "Hello", "red", 0, "yes"], [2, "Hello", "red", 1, "no"], [3, "Hello", "blue", 4, "no"], [4, "Sunshine", "yellow", 5, "yes"], [5, "Hello", "red", 6, "yes"]], 
    result = data.filter(uniqueBy(key([1, 2, 4])));
    
console.log(result);
.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