在elm中可以使用字符串访问记录吗?

在elm中可以使用字符串访问记录吗?,elm,Elm,如果我有一个字符串,看起来像记录中某个字段的名称,我可以用它来获取数据吗?比如: ."name".toKey bill bill.(asSymbol "name") - 不,但是你可以用一个 这不是您想要的方式,但您可以使用模式匹配字符串的函数来访问记录的一部分。您需要明确说明它应该做什么,以防您给它一些无效的东西 import Html exposing (..) type alias Song = { artist : String, number : String, title :

如果我有一个字符串,看起来像记录中某个字段的名称,我可以用它来获取数据吗?比如:

."name".toKey bill 
bill.(asSymbol "name")
-

不,但是你可以用一个


这不是您想要的方式,但您可以使用模式匹配字符串的函数来访问记录的一部分。您需要明确说明它应该做什么,以防您给它一些无效的东西

import Html exposing (..) 

type alias Song = { artist : String, number : String, title : String }

song : Song
song =
  { title = "foot", artist = "barf", number = "13" }

fieldsILike : List String
fieldsILike =
  [ "title", "artist" ]


k2acc : String -> (Song -> String)
k2acc key = 
  case key of 
  "title" ->  .title
  "artist" -> .artist
  "number" -> .number
  _ ->  always ""

val : Song -> String -> String
val = flip k2acc 
-- `flip` was removed in elm 0.19, so you'll need to 
-- do something like the following going forward: 
-- val song field = (k2acc field) song   

foo = List.map (val song) fieldsILike

main = text <| toString foo
导入Html公开(…)
键入alias Song={艺术家:字符串,编号:字符串,标题:字符串}
歌曲:歌曲
歌=
{title=“foot”,artist=“barf”,number=“13”}
fieldsILike:列表字符串
野性=
[“头衔”、“艺术家”]
k2acc:String->(歌曲->字符串)
k2acc键=
案例关键
“标题”->。标题
“艺术家”->,艺术家
“编号”->。编号
_->始终“”
瓦尔:歌曲->字符串->字符串
val=翻转k2acc
--“flip”已在elm 0.19中删除,因此您需要
--继续执行以下操作:
--val song字段=(k2acc字段)song
foo=List.map(val-song)类似字段

main=text我通过在
AttrValue
type:

type alias TypedRecord =
    List Attr

type alias Attr =
    ( String, AttrValue )


type AttrValue
    = String String
    | Int Int
    | Record (List Attr)
getAttrByKey : String -> TypedRecord -> Maybe Attr
getAttrByKey searchKey item =
    -- imitation of searching for attributes likewise in JS Record
    let
        checkAttrKey =
            \k attr ->
                first attr == k
    in
    case String.split "." searchKey of
        [ key ] ->
            List.head <| List.filter (checkAttrKey key) item

        key :: rest ->
            case List.head <| List.filter (checkAttrKey key) item of
                Just attr ->
                    case attr of
                        ( _, Record subAttr ) ->
                            getAttrByKey (String.join "." rest) subAttr

                        ( _, _ ) ->
                            Nothing

                Nothing ->
                    Nothing

        [] ->
            Nothing
因此,现在我可以通过
“key”
(对于嵌套,甚至
“key.key”
)从我的
类型记录中检索属性
类型:

type alias TypedRecord =
    List Attr

type alias Attr =
    ( String, AttrValue )


type AttrValue
    = String String
    | Int Int
    | Record (List Attr)
getAttrByKey : String -> TypedRecord -> Maybe Attr
getAttrByKey searchKey item =
    -- imitation of searching for attributes likewise in JS Record
    let
        checkAttrKey =
            \k attr ->
                first attr == k
    in
    case String.split "." searchKey of
        [ key ] ->
            List.head <| List.filter (checkAttrKey key) item

        key :: rest ->
            case List.head <| List.filter (checkAttrKey key) item of
                Just attr ->
                    case attr of
                        ( _, Record subAttr ) ->
                            getAttrByKey (String.join "." rest) subAttr

                        ( _, _ ) ->
                            Nothing

                Nothing ->
                    Nothing

        [] ->
            Nothing
这些示例用于
String
Int
Record
Elm类型,但也可以扩展为
Float
Bool
Array
。 您可以检查
src/lib/TypedRecord.elm
文件中的其他函数,甚至是my中的Json解码器

attrToString : Maybe Attr -> String
attrToString is_attr =
    case is_attr of
        Nothing ->
            ""

        Just attr ->
            case second attr of
                String value ->
                    value

                Int value ->
                    String.fromInt value

                Record attrs ->
                    List.map (\a -> Just a) attrs
                        |> List.map (\a -> attrToString a)
                        |> String.join " "