如何使用find all根据条件显示整个数组?

时间:2017-10-21 14:53:58

标签: groovy

我无法根据条件在groovy中输出整个数组。实际上我想在下面做的是,如果数组中的名字='彼得',则输出属于彼得的所有信息。目前我可以让它显示唯一相关的Pater的第一个名字但是如何输出who数组信息?

目前几乎输出:[Peter,Peter,Peter]

我希望它输出:[1,Peter,Smith],[2,Peter,Jonea],[4,Peter,Patel]

以下代码:

def array = [[number: 1, forename: "Peter" , surname: "Smith"],
            [number: 2, forename: "Peter" , surname: "Jones"],
            [number: 3, forename: "Tom" , surname: "White"],
            [number: 4, forename: "Peter" , surname: "Patel"],
            [number: 5, forename: "George" , surname: "Woodburn"]]


def arrayForenameFindAll = array.forename.findAll{it == "Peter"}

1 个答案:

答案 0 :(得分:2)

def array = [[number: 1, forename: "Peter" , surname: "Smith"],
        [number: 2, forename: "Peter" , surname: "Jones"],
        [number: 3, forename: "Tom" , surname: "White"],
        [number: 4, forename: "Peter" , surname: "Patel"],
        [number: 5, forename: "George" , surname: "Woodburn"]]


def arrayForenameFindAll = array.findAll { it.forename == 'Peter' }*.values()

这将评估问题中描述的List List