4

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

4
  • Kindly edit to show your current code Commented Jul 8, 2022 at 18:48
  • Now I am a medium lvl or you can say noob. I tried from internet but can not solve the issue you can ignore my code other. Commented Jul 8, 2022 at 18:57
  • Kindly edit to show log output or what error it throws or what did you expect and what did it not do? A description of the issue with your current code. See minimal reproducible example Commented Jul 8, 2022 at 18:57
  • it says unkown error Commented Jul 8, 2022 at 19:00

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Your mind must be twisted in closures:)
@TheMaster, just a bad lisp (rpl) habit.
@NinaScholz In the world of functional programming that's considered best practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.