I've seen this, but it doesn't return the desired result, which would be:
[
[4, "frente", 196],
[5, "frente", 196]
]
function getUniqueData(arr = [
[4, "frente", 196],
[4, "frente", 196],
[5, "frente", 196]
], uniqueCols = [0]) {
const uniqueData = arr.filter((currentRow, i) => {
const currentCombo = uniqueCols.map(index => currentRow[index]);
return !arr.some((row, j) => {
const combo = uniqueCols.map(index => row[index]);
return combo.every((c1, k) => c1 === currentCombo[k]) && i !== j;
});
});
return uniqueData;
}
console.log(getUniqueData())
CodePudding user response:
Try this:
function getUniqueData(arr = [[4, "frente", 196], [4, "frente", 196], [5, "frente", 196]]) {
let uA = [];
let jA = []
arr.forEach((r, i) => {
if (!~jA.indexOf(arr[i].join())) {
uA.push(r);
jA.push(arr[i].join())
}
});
Logger.log(uA);
}
Execution log
2:32:02 PM Notice Execution started
2:32:03 PM Info [[4.0, frente, 196.0], [5.0, frente, 196.0]]
2:32:04 PM Notice Execution completed
CodePudding user response:
From what I understand you are trying to remove duplicates. What you have tried removes all the entries that have the same column.
Here is an answer based on a similar thing but for objects
Here I filter out entries based on unique column combinations
Hope this was what you meant
function getUniqueData(arr = [
[4, "frente", 196],
[4, "frente", 196],
[5, "frente", 196]
], uniqueCols = [0]) {
const uniqueData = arr.filter((currentRow, i, self) => {
return i === self.findIndex((t) => {
return uniqueCols.every((col) => t[col] === currentRow[col])
})
});
return uniqueData;
}
console.log(getUniqueData())
more cases
function getUniqueData(arr = [
[4, "frente", 196],
[4, "frente", 196],
[5, "frente", 196]
], uniqueCols = [1]) {
const uniqueData = arr.filter((currentRow, i, self) => {
return i === self.findIndex((t) => {
return uniqueCols.every((col) => t[col] === currentRow[col])
})
});
return uniqueData;
}
console.log(getUniqueData())
function getUniqueData(arr = [
[4, "a", 196],
[4, "frente", 196],
[5, "frente", 196]
], uniqueCols = [1]) {
const uniqueData = arr.filter((currentRow, i, self) => {
return i === self.findIndex((t) => {
return uniqueCols.every((col) => t[col] === currentRow[col])
})
});
return uniqueData;
}
console.log(getUniqueData())
function getUniqueData(arr = [
[4, "frente", 196],
[4, "frente", 196],
[5, "frente", 196]
], uniqueCols = [0,1]) {
const uniqueData = arr.filter((currentRow, i, self) => {
return i === self.findIndex((t) => {
return uniqueCols.every((col) => t[col] === currentRow[col])
})
});
return uniqueData;
}
console.log(getUniqueData())