2

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())
Rubén
  • 34,714
  • 9
  • 70
  • 166
onit
  • 2,275
  • 11
  • 25

2 Answers2

1

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
Cooper
  • 59,616
  • 6
  • 23
  • 54
0

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())
cmgchess
  • 7,996
  • 37
  • 44
  • 62