简体   繁体   English

删除对象数组中的特定元素

[英]Remove specific elements in Array of objects

I have an Array of Object that is declared and look like this in Groovy:我有一个 Object 数组,它在 Groovy 中被声明并看起来像这样:

def meal = [['Apple','Tomatoes', 'Coffee'],['Peach','Broccoli', 'Water'],['Orange','Peas', 'Pepsi'],['Banana','Beans', 'Coffee']]

How could I remove the third element in each object to my output look like this:如何将每个 object 中的第三个元素删除到我的 output 中,如下所示:

def meal = [['Apple','Tomatoes'],['Peach','Broccoli'],['Orange','Peas'],['Banana','Beans']] def 餐 = [['苹果','西红柿'],['桃子','西兰花'],['橙子','豌豆'],['香蕉','豆子']]

I tried this but it returns a boolean (true)我试过了,但它返回 boolean(真)

def removeValue = meal.remove{a ->
a[2]
a}

def meal = [['Apple','Tomatoes', 'Coffee'],['Peach','Broccoli', 'Water'],['Orange','Peas', 'Pepsi'],['Banana','Beans', 'Coffee']] def 餐 = [['苹果','西红柿', '咖啡'],['桃子','西兰花', '水'],['橙子','豌豆', '百事可乐'],['香蕉' ,'豆子', '咖啡']]

meal.remove(meal[2])餐.删除(餐[2])

println(meal) \ [[Apple, Tomatoes, Coffee], [Peach, Broccoli, Water], [Banana, Beans, Coffee]] println(meal) \ [[苹果、西红柿、咖啡]、[桃子、西兰花、水]、[香蕉、豆类、咖啡]]

remove method creates a copy of given list without given index or condition(for closure) value remove 方法在没有给定索引或条件(用于关闭)值的情况下创建给定列表的副本

dropRight and splicing and maybe others can be used to get rid of elements at the end of a vector and give you back a new vector. dropRight和拼接,也许还有其他方法可以用来去掉向量末尾的元素,并给你一个新的向量。 Using the Spread-Operator *.使用扩展运算符*. to do that on all.做到这一点。

meal*.dropRight(1)

If you prefer modifying your original, there is also removeLast .如果您更喜欢修改您的原件,还有removeLast

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM