从数组数组中删除重复对

时间:2021-05-14 21:22:45

I have an array of pairs like the example below:

我有一组对,如下例所示:

array = [["human KIR2DS1", 446.0], ["mouse BMP-4", 446.0], ["mouse BMP-4", 446.0], ["mTIMP2 lot DAAP01", "435a"], ["hKIR3DL3 lot DDBL01", "435a"]]

I want to remove the duplicate pairs in the array. What is the shortest method to do this?

我想删除数组中的重复对。这样做的最短方法是什么?

2 个解决方案

#1


3  

Use Array#uniq:

使用Array #uniq:

array.uniq
# => [["human KIR2DS1", 446.0], ["mouse BMP-4", 446.0], ["mTIMP2 lot DAAP01", "435a"], ["hKIR3DL3 lot DDBL01", "435a"]]

or if you want to modify the original array:

或者如果要修改原始数组:

array.uniq!
array # => [["human KIR2DS1", 446.0], ["mouse BMP-4", 446.0], ["mTIMP2 lot DAAP01", "435a"], ["hKIR3DL3 lot DDBL01", "435a"]]

#2


0  

also this can be used to sort out the duplicate and return a new Set of array wihtout duplicate. I use the spread operator "..."

这也可以用来整理副本并返回一个没有重复的新数组。我使用传播运算符“......”

function unique(array) {
    return [...new Set(array)];
}

#1


3  

Use Array#uniq:

使用Array #uniq:

array.uniq
# => [["human KIR2DS1", 446.0], ["mouse BMP-4", 446.0], ["mTIMP2 lot DAAP01", "435a"], ["hKIR3DL3 lot DDBL01", "435a"]]

or if you want to modify the original array:

或者如果要修改原始数组:

array.uniq!
array # => [["human KIR2DS1", 446.0], ["mouse BMP-4", 446.0], ["mTIMP2 lot DAAP01", "435a"], ["hKIR3DL3 lot DDBL01", "435a"]]

#2


0  

also this can be used to sort out the duplicate and return a new Set of array wihtout duplicate. I use the spread operator "..."

这也可以用来整理副本并返回一个没有重复的新数组。我使用传播运算符“......”

function unique(array) {
    return [...new Set(array)];
}