_.uniq_在Ruby中使用Array.compact和Array.uniq方法从Array中移除元素

_.uniq

Ruby Array.compact和Array.uniq方法 (Ruby Array.compact and Array.uniq Methods)

In the last article, we have gone through two different methods of deleting elements from the Array. We have seen their implementation with the help of their syntaxes and supporting examples. For a quick recall, let me tell those two methods were Array.delete() and Array.delete_at(). Array.delete() is a method that requires the name of the element which is supposed to be passed in the argument list of the Array. It works in a way that it deletes all the elements of the same name as mentioned in the argument list of Array.delete() whereas Array.delete_at() requires the index of the element we want to remove from the Array.

在上一篇文章中,我们介绍了从Array删除元素的两种不同方法 。 我们已经在它们的语法和支持示例的帮助下看到了它们的实现。 为了快速回顾一下,让我说说这两个方法是Array.delete()和Array.delete_at()Array.delete()是一种方法,它需要假定应该在Array的参数列表中传递的元素的名称。 它的工作方式是删除与Array.delete()的参数列表中提到的名称相同的所有元素,而Array.delete_at()则需要我们要从Array中删除的元素的索引。

In the article, we will learn about two more and important methods of deleting the elements from the instance of Array class namely Array.comp() and Array.uniq()!.

在本文中,我们将学习另外两个重要的删除Array类实例中元素的方法,即Array.comp()和Array.uniq()!

Now let us go through their implementation with the help of their syntaxes and examples which you will find in the rest of the article.

现在,让我们在它们的语法和示例的帮助下完成它们的实现,您将在本文的其余部分中找到它们。

1)Array.compact方法 (1) Array.compact Method)

Parameter(s):

参数:

This method requires no arguments.

此方法不需要任何参数。

Syntax:

句法:

    Array.compact
    # or
    Array.compact!

Method description:

方法说明:

In most of the cases, you will find that your array is containing some nil values and you will be requiring some mechanism or lines of code to delete those nil values which are present in your Array. Ruby decreases your overhead of writing code for the same purpose by providing you a method named Array.compact. This method is used to remove all the nil values from the object of the Array class. Array.compact! is used to make changes in the Array as well.

在大多数情况下,您会发现数组中包含一些nil值,并且需要某种机制或代码行来删除数组中存在的这些nil值。 Ruby通过为您提供一个名为Array.compact的方法来减少出于相同目的而编写代码的开销。 此方法用于从Array类的对象中删除所有nil值。 Array.compact! 也用于在Array中进行更改。

Example:

例:

=begin
    Ruby program to remove elements from Array 
    using Array.compact
=end

# array declaration
Adc = [nil,'Includehelp.com','Ruby','C++','C#','Java','Python','C++',nil,nil]

# Array.compact method
puts "Array.compact!"
Adc.compact

# printing array elements
puts "Array elements are:"
print Adc
puts ""

# Array.compact! method
puts "Array.compact!"
Adc.compact!

# printing array elements
puts "Array elements are:"
print Adc

Output

输出量

Array.compact!
Array elements are:
[nil, "Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++", nil, nil]
Array.compact!
Array elements are:
["Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++"]

Explanation:

说明:

In the above code, you can observe that Array.compact can be used to remove nil elements from the Array whereas Array.compact! can be used to make changes in the same Array as well.

在上面的代码中,您可以观察到Array.compact可用于从Array中删除nil个元素,而Array.compact! 也可以用于在同一Array中进行更改。

2)Array.uniq方法 (2) Array.uniq Method)

Parameter (s):

参数:

No arguments required.

无需参数。

Syntax:

句法:

    Array.uniq
    # or
    Array.uniq!

Method description:

方法说明:

Array.uniq or Array.uniq! can be used to remove duplicate elements from the Array. Sometimes you may observe that your Array is containing duplicate entries which may create any kind of complexity. Array.uniq is non-destructive whereas Array.uniq! is destructive, which means that the latter will create changes in the same Array as well.

Array.uniqArray.uniq! 可用于从数组中删除重复的元素。 有时,您可能会发现您的数组包含重复的条目,这可能会造成任何形式的复杂性。 Array.uniq是非破坏性的,而Array.uniq! 是破坏性的,这意味着后者也会在同一Array中创建更改。

Example:

例:

=begin
    Ruby program to remove elements from Array 
    using Array.uniq
=end

# array declaration
Adc = ['Ruby','Includehelp.com','Ruby','C++','C#','Java','Python','C++','Java']

# Array.uniq Method
puts "Array.uniq"
print Adc.uniq
puts ""
puts "Array elements are:"
print Adc
puts ""

# Array.uniq! Method
puts "Array.uniq!"
Adc.uniq!
puts "Array elements are:"
print Adc

Output

输出量

Array.uniq
["Ruby", "Includehelp.com", "C++", "C#", "Java", "Python"]
Array elements are:
["Ruby", "Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++", "Java"]
Array.uniq!
Array elements are:
["Ruby", "Includehelp.com", "C++", "C#", "Java", "Python"]

Explanation:

说明:

In the above code, you can see that Array.uniq is not creating any change in the Array whereas it is possible with the help of Array.uniq! method. You can go for Sets if you want to avoid duplicacy.

在上面的代码中,您可以看到Array.uniq并没有在Array中创建任何更改,而借助Array.uniq可以实现! 方法。 如果要避免重复,可以选择Sets。

翻译自: https://www.includehelp.com/ruby/removing-elements-from-the-array-using-of-array-compact-and-array-uniq-methods.aspx

_.uniq

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值