Insert 'and' before last item in a list

219
3
Jump to solution
05-20-2019 01:54 PM
deleted-user-6r8IUoC9zS9o
New Contributor

Using Arcade I have created narrative conventions for different fields in my dataset. I would like to concatenate these features into a sentence separated by a comma, based on their presence in each feature. That's simple enough, but I would like to place 'and' after the comma before the last item in the list, if that comma is even necessary.

.e.g., a feature is populated for Transit. but not Car, Bike, or Ped, so the concatenated sentence would read "...people taking transit."

or

.e.g., a feature is populated for Car and Ped, but not Bike or Transit, so the concatenated sentence would read "...people driving and people walking."

or

e.g., a feature is populated for Car, Bike, Ped, and Transit, so the sentence would read "... people driving, people riding bicycles, people walking, and people taking transit."

var Car = IIF(IsEmpty($feature.Car),"",'people driving')
var Bike = IIF(IsEmpty($feature.Bike),"",'people riding bicycles')
var Ped = IIF(IsEmpty($feature.Ped),"",'people walking')
var Transit = IIF(IsEmpty($feature.Transit),"",'people taking transit')‍‍‍‍

I get the feeling this can be done through an array, but I haven't been able to figure out the logic. 

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

This is how you could do thisYou can omit the first 4 lines, and replace on line 7 every "feature_" by "$feature.".

var feature_Car = Null; // feature_Car should be $feature.Car
var feature_Bike = Null;
var feature_Ped = Null;
var feature_Transit = Null;

var array0 = ['people driving', 'people riding bicycles', 'people walking', 'people taking transit'];
var array1 = [feature_Car, feature_Bike, feature_Ped, feature_Transit];
var array2 = [];
for (var i in array1) {
    if (!IsEmpty(array1[i])) {
        array2[Count(array2)] = array0[i];
    }
}

Console(array0);
Console(array1);
Console(array2);

var result = "";
var array3 = [];

if (Count(array2) == 1) {
    result = array2[0];
} else if (Count(array2) > 1) {
    for (i in array2) {
        if (i < Count(array2) - 2) {
            array3[Count(array3)] = array2[i];
            array3[Count(array3)] = ", ";
        } else if (i == Count(array2) - 2) {
            array3[Count(array3)] = array2[i];
            array3[Count(array3)] = " and ";
            array3[Count(array3)] = array2[i+1];
        } else {
            // 
        }
    }
    result = Concatenate(array3, "");
}

Console(array3);
Console(result);

Return result;

View solution in original post

3 Replies
XanderBakker
Esri Esteemed Contributor

This is how you could do thisYou can omit the first 4 lines, and replace on line 7 every "feature_" by "$feature.".

var feature_Car = Null; // feature_Car should be $feature.Car
var feature_Bike = Null;
var feature_Ped = Null;
var feature_Transit = Null;

var array0 = ['people driving', 'people riding bicycles', 'people walking', 'people taking transit'];
var array1 = [feature_Car, feature_Bike, feature_Ped, feature_Transit];
var array2 = [];
for (var i in array1) {
    if (!IsEmpty(array1[i])) {
        array2[Count(array2)] = array0[i];
    }
}

Console(array0);
Console(array1);
Console(array2);

var result = "";
var array3 = [];

if (Count(array2) == 1) {
    result = array2[0];
} else if (Count(array2) > 1) {
    for (i in array2) {
        if (i < Count(array2) - 2) {
            array3[Count(array3)] = array2[i];
            array3[Count(array3)] = ", ";
        } else if (i == Count(array2) - 2) {
            array3[Count(array3)] = array2[i];
            array3[Count(array3)] = " and ";
            array3[Count(array3)] = array2[i+1];
        } else {
            // 
        }
    }
    result = Concatenate(array3, "");
}

Console(array3);
Console(result);

Return result;
deleted-user-6r8IUoC9zS9o
New Contributor

Thanks for the solution! Very helpful

XanderBakker
Esri Esteemed Contributor

You're welcome!

0 Kudos