如何从数组中删除一定范围的值? [英] How to delete a range of values from an array?

查看:86
本文介绍了如何从数组中删除一定范围的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果array = [1, 2, 3, 4, 5, 6, 7, 8, 9], 我想从数组中删除一系列元素.

If array = [1, 2, 3, 4, 5, 6, 7, 8, 9], I want to delete a range of elements from array.

例如:我想从该数组中删除索引范围为2..5的所有元素,结果应为[1, 2, 7, 8, 9]

For example: I want to delete all elements with an index in the range 2..5 from that array, the result should be [1, 2, 7, 8, 9]

谢谢.

推荐答案

使用

删除[...]给定范围内的元素.

Deletes the element(s) given by [...] a range.

array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
array.slice!(2..5)
array #=> [1, 2, 7, 8, 9]

这篇关于如何从数组中删除一定范围的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆