Home > database >  Remove duplicate certain column values Javascript 2D array
Remove duplicate certain column values Javascript 2D array

Time:07-14

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();

CodePudding user response:

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

  • Related