Question:
So, Basically what I want is to get unique data from a 2d array where I can pass multiple column values which I have stored in an array. Like I want to get unique based on column 1 and 2 so, I can pass the column value like an array [0, 1] to the function and based on the column array it should return unique value.I have tried something like this:
Answer:
Solution:
- Use filter to remove the undesired elements from your array.
- For each row, use map to get the combination of values related to the columns you want to check, so that if another row exists with this combination, both rows should be filtered out.
- For each row, use some to check whether there is another row with this same combination.
- Use every to compare both combinations and check they are the same.
Code sample:
Note:
In the sample above, I added the example you provided in comments
data = [[1, 2, 3],[1, 2, 2],[1, 2, 3],[2, 1, 3]] and I want to check unique in column 1
as default parameters. In this case, the returned uniqueData
is [2, 1, 3]
, as expected.If you have better answer, please add a comment about this, thank you!