遍历列表以通过其名称获取值

Iterate over list to get value by its name

这行得通。但是还有更多的efficient/simpler方法可以得到output吗?

test_list <- list(list("name"="A","property"=1),
              list("name"="B","property"=2),
              list("name"="C","property"=3))

myFunction <- function(arg1=NULL, arg2=NULL){
  arg1[[arg2]]
}

# works
output <- sapply(test_list, myFunction, "property")

# returns NULL 
# output <- sapply(test_list, `$`, "property")

我们可以指定匿名函数调用来进行提取

sapply(test_list, function(x) x$property)
#[1] 1 2 3