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