How do I determine if a column name exists in an array or datasubset?

How do I determine if a column name exists in an array or datasubset?

Say for example, the below expression when executed returns that datasubset has two fields /columns names - firstName and secondName. I want to find out if any string "xyz" is present as a field or column in a datasubset /array.

todatasubset({
type!Column(field: "username", alias: "un", visible: true),
type!Column(field: "firstName", alias: "first", visible: false),
type!Column(field: "lastName", alias: "last", visible: true)
},
a!pagingInfo(startIndex: 1, batchSize: 2, sort:{field: "alias", ascending: true})
)
.data

when executed returns the below result

[field=firstName, alias=first, visible=false], [field=lastName, alias=last, visible=true]

OriginalPostID-152071

OriginalPostID-152071

  Discussion posts and replies are publicly visible

  • You can store the result of the todatasuset into the local variable & print the same variable on the screen. It will list the detail info of it.
  • 0
    Certified Associate Developer
    If I understand your question, I think comparing index( local!varName , "xyz" , null() ) to null() might help.
  • What about using fn!contains(array, value)? See the below test code.
    = load(
    local!datasubset: todatasubset(
    {
    type!Column(
    field: "username",
    alias: "un",
    visible: true
    ),
    type!Column(
    field: "firstName",
    alias: "first",
    visible: false
    ),
    type!Column(
    field: "lastName",
    alias: "last",
    visible: true
    )
    },
    a!pagingInfo(
    startIndex: 1,
    batchSize: -1,
    sort: {
    field: "alias",
    ascending: false
    }
    )
    ).data,
    {
    a!textField(
    label: "Test column exists or not",
    value: if(fn!contains(local!datasubset.field,"lastName"),
    "The column exists",
    "The column is missing"),
    readOnly: true
    ),
    a!textField(
    label: "all columns",
    value: local!datasubset.field,
    readOnly: true
    )
    }
    )
  • I think you might want to know how to check if a datasubset contains a column to show on the screen. Since you set batch size is 2, so only 2 columns will be shown. If so, you can do it in the following way.
    = load(
    local!datasubset: todatasubset(
    {
    type!Column(
    field: "username",
    alias: "un",
    visible: true
    ),
    type!Column(
    field: "firstName",
    alias: "first",
    visible: false
    ),
    type!Column(
    field: "lastName",
    alias: "last",
    visible: true
    )
    },
    a!pagingInfo(
    startIndex: 1,
    batchSize: 2,
    sort: {
    field: "alias",
    ascending: true
    }
    )
    ).data,
    local!displayColumns: local!datasubset,
    {
    a!textField(
    label: "Test column exists or not",
    value: if(
    fn!contains(
    local!displayColumns.field,
    "username"
    ),
    "The column exists",
    "The column is missing"
    ),
    readOnly: true
    ),
    a!textField(
    label: "all columns",
    value: local!datasubset.field,
    readOnly: true
    )
    }
    )
  • 0
    Certified Lead Developer
    You may wish to include an index to handle null data subsets:

    if(
              fn!contains(
                        index(
                                  local!displayColumns
                                  "field",
                                  {}
                        )
                        "username"
              ),
              "The column exists",
              "The column is missing"
    ),