Skip to main content

Use DekiScript lists and maps

This page applies to:MindTouch Responsive

This page demonstrates all the basic operations you'd ever want to do to list and map elements.  If you do any serious Dekiscripting, you'll probably end up needing to use most of all of these.

Create lists and maps

This is simple stuff.  We'll use these examples as fodder for our various manipulations later on this page.


var myList = [ "zero", "one", "two", "three" ];


var myMap = { one:1, two:2, three:3 };

If you want to use a key name stored in a variable, simply enclose it in parentheses:

var keyName = "one";
var myMap = { (keyName):1, two:2, three:3 };

Accessing elements

By "accessing", we mean using an element in a right-side expression. These forms are not used to set element values, only read them.


Accessing a list element is straightforward:

var listElement = myList[0]; // = "zero"

Of course, the subscript can be any expression:

var subscript = 0;
var listElement = myList[subscript+1]; // = "one"


There are two ways to access map elements. If the key is a known constant:

var mapElement =;   // = 1

If the keyname is stored in a variable, then use list-like syntax ("bracket notation"):

var keyName = "one";
var mapElement = myMap[keyName]; // = 1

Of course you can use bracket notation with a constant keyname as well, if you're so inclined; just remember to enclose the constant in quotes:

var mapElement = myMap["one"]; // = 1

Size of a list or map

For both lists and maps, the syntax for finding the number of elements is the same:

var myListLength = #myList;
var myMapLength = #myMap;

For a list, then, the easiest way to access the last element is:

var myLastElement = myList[#myList-1];

The list.splice() function

We now need to take a moment to talk about the list.splice() built-in function, which is going to get a workout when we start modifying lists below. In the examples below. This function does two things:

  1. Remove element(s) if desired from a specified index in a list.
  2. Insert element(s) if desired to that same index.

The key thing to remember here is that the insertions and deletions occur at the same index. By combining insertions and deletions, we can do most of what we'd want, even though it wouldn't necessarily be the first thing we'd think to try.

Adding New Elements

The primary (but not the only) way to add a new element to either a list or map is to concatenate a list with the new element(s).


To add a new element to a list:

let myList ..= [ "four" ];

Of course you can add more than one at a time:

let myList ..= [ "four", "five" ];

We can also use the all-powerful list.splice() function if we want to add new item(s) to a specific point in the list:

{{  var myList = [ "one", "two", "three", "four" ];
    let myList = list.splice(myList, 1, 0, [ "one point five" ]);
[ "one", "one point five", "two", "three", "four" ]


The approach is similar:

let myMap ..= { four:4 };
let myMap ..= { five:5, six:6 };

And remember that the keyname can be an expression if enclosed in parentheses:

var keyName = "four";
let myMap ..= { (keyName):4 };

Modifying Existing Elements


Modification of list elements can be done with list.splice(). By removing an element and then inserting a new one at the same location, we can get the job done. To capitalize the item at index 2:

let myList = list.splice(myList, 2, 1, [ toupper(myList[2]) ]);

That's a little ungainly, but it works.


You can modify a map element by concatenating a list with the new value:

let myMap ..= { one:"uno" };

If the key already exists, this will re-assign the value. So I could increment a numeric value like this:

let myMap ..= { };

Deleting Elements


There are two ways to remove elements from a list. If you'd like to so based on the index (or indices) of the elements, then use list.splice().  Let's remove the second element from our list:

let myList = list.splice(myList, 1, 1);

To remove elements based on their value, use Let's remove all elements with value "one":

let myList =, "$ != 'one'");


For maps, we'll have to depend on  To remove one element, we'll define a test that matches on everything but the one we want to remove. So let's remove element "one" based on its key:

let myMap =, "$.key != 'one'");

Now let's remove all elements with the value "1":

let myMap =, "$.value != 1");

Not very pretty, but it gets the job done in a pinch.  Of course, you can do a lot more with list.splice(),, and then just remove individual items from a list or map.

Conversion between Lists and Maps

There are a lot of ways to convert between lists and maps; the best way depends on the exact structure of your list or map, and what you're trying to accomplish. Here are some common examples.

To perform a basic conversion from a map to a list, use map.keyvalues(). This creates a list of maps as follows:

{{  var myMap = { one:1, two:2, three:3 };
    var myList = map.keyvalues(myMap);
[ { key : "one", value : 1 }, { key : "two", value : 2 }, { key : "three", value : 3 } ]

This is actually a pretty useful form, since we can then use list.sort() to sort it by value.

Going from a list to a map is actually a bit trickier, and heavily dependent on what you're trying to do. Most solutions will require iterating through the list, and creating a map entry for each. Here's an example:

{{  var myList = [ "zero", "one", "two", "three" ];
    var myMap = {};
    foreach (var e in myList) {
      let myMap ..= { (e):__count };
    "myList="; myList; web.html("<br   />");
    "myMap="; myMap;
myList=[ "zero", "one", "two", "three" ]
myMap={ one : 1, three : 3, two : 2, zero : 0 }

Other list and map tips and tricks

Getting distinct values from a list

There is no built-in function to get distinct values in a list like you can in SQL. However, by combining some list and map techniques you can do this:

  var myList = [1,3,3,4,2,2,5];

[ "1", "3", "4", "2", "5" ]

Note that this will always return a list of strings, since the keys of the map that list.groupby() returns are always strings. If your original list is filled with strings, or if you don't care about the type of the returned values, that's no problem.

If you want to retain the type of the original data, you can use this slightly more complex approach, using a list constructor:

  var myList = [1,3,3,4,2,2,5];

  [ k[0] foreach var k in List.Groupby(myList,"$") ];
[ 1, 3, 4, 2, 5 ]

Remember that when we iterate over a map, DekiScript automatically gives us the values, which is why we didn't need to use a map.values() call in the second example.

  • Was this article helpful?