The functions below work for ArcGIS Enterprise 10.8 and later. I have used these two functions quite often since these automatically account for matching field names but don't account for field types.
// Get matching field names found in both lists
function GetMatchingFields( FieldListA, FieldListB ){
var MatchingFields = Null
var separator = ','
// Loop through the list of field names in list A
for ( var a in FieldListA ){
for ( var b in FieldListB ){
if ( FieldListA[ a ] == FieldListB[ b ] ){ MatchingFields += FieldListA[ a ] + separator }
}
}
if ( IsEmpty( MatchingFields ) == False ){ MatchingFields = split( MatchingFields , separator ) }
return MatchingFields
}
// Get field names from input features
function GetFieldNames( InputDictionary , OmitFields ){
var FieldNames = Null
var separator = ','
for ( var field in InputDictionary ){
// Populate the specified list with the field name
var keep = True
for ( var i in OmitFields ){
if ( field == OmitFields[ i ] ){ keep = False ; Break }
}
if ( keep == True ){ FieldNames += field + ',' }
}
if ( IsEmpty( FieldNames ) == False ){ FieldNames = split( FieldNames , separator ) }
return FieldNames
}
The functions below were updated to use on Enterprise 10.9 and later.
// Get matching field names found in both lists
function GetMatchingFields( FieldListA, FieldListB ){
var MatchingFields = []
// Loop through the list of field names in list A
for ( var a in FieldListA ){
if ( Includes( FieldListB , a ) ){ Push( MatchingFields , a ) }
}
if ( Count( MatchingFields ) == 0 ){ MatchingFields = Null }
return MatchingFields
}
// Get field names from input features
function GetFieldNames( InputFeature , OmitFields ){
if( TypeOf( InputFeature ) == 'Feature' ){ InputFeature = First( InputFeature ) }
var FieldNames = []
for ( var field in InputFeature ){
// Populate the specified list with the field name
if ( !Includes( OmitFields , field ) ){ Push( FieldNames , field ) }
}
if ( Count( FieldNames ) == 0 ){ FieldNames = Null }
return FieldNames
}
It would be nice if functions like these were added to the library of functions in arcade since it is fairly standard and would be easier to use.