根据一组索引删除数组的内容 [英] Delete contents of array based on a set of indexes
本文介绍了根据一组索引删除数组的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
delete_at 只需要一个索引.使用内置方法实现这一目标的好方法是什么?不一定是集合,也可以是索引数组.
delete_at only takes a single index. What's a good way to achieve this using built-in methods? Doesn't have to be a set, can be an array of indexes as well.
arr = ["a", "b", "c"]
set = Set.new [1, 2]
arr.delete_at set
# => arr = ["a"]
推荐答案
One-liner:
arr.delete_if.with_index { |_, index| set.include? index }
这篇关于根据一组索引删除数组的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文