> Erlang中文手册 > get_all_values/2 获取指定键的所有值

proplists:get_all_values/2

获取指定键的所有值

用法:

get_all_values(Key, List) -> [term()]

内部实现:

-spec get_all_values(Key, List) -> [term()] when
      Key :: term(),
      List :: [term()].

get_all_values(Key, [P | Ps]) ->
    if is_atom(P), P =:= Key ->
	    [true | get_all_values(Key, Ps)];
       tuple_size(P) >= 1, element(1, P) =:= Key ->
	    case P of
		{_, Value} ->
		    [Value | get_all_values(Key, Ps)];
		_ ->
		    get_all_values(Key, Ps)
	    end;
       true ->
	    get_all_values(Key, Ps)
    end;
get_all_values(_Key, []) ->
    [].

类似 proplists:get_value/2,但是它返回列表 List 里所有格式为 {Key, Value} 的条目的值的列表。如果不存在这种条目,则返回空列表。

List = [{a, 1}, {b, 2}, {b, 3}, {c, 4}, {b, 5}, {d, 6}],
proplists:get_all_values(b, List).