Arrays

Last updated: Mar 5th, 2018

Introduction

Arrays in Cuneiform are very similar to arrays in JavaScript Object Notation. Array values must be of the following types:

  • string
  • number
  • object
  • array
  • null

Arrays are declared within square brackets ([]). For example:


var cars = ["Tesla", "Volvo", "BMW"];
                                    

Accessing Array Values

Array values can be accessed by using the index of the element in the array within square brackets ([]).


var cars = ["Tesla", "Volvo", "BMW"];
var firstCar = cars[0];
                                    

Arrays in Cuneiform start with the index 0.

Array operations

Append

The append operation allows an additional value to be added into the array. Its syntax is as follows.

arrayName.append(value);

In th example below, we shall add another value to the cars array.


var cars = ["Tesla", "Volvo", "BMW"];
cars.append("Toyota");
                                    

The cars array now has the following values: "Tesla", "Volvo", "BMW", "Toyota".

Remove

The remove operation removes a value from an array. In order to do so, the index of the element that is to be removed must be provided. The syntax is as follows.

arrayName.remove(index);

In the example below, we shall remove Volvo from our array of cars. Since Volvo is the element in the 1st index of the cars array, the remove operation is executed as follows.


var cars = ["Tesla", "Volvo", "BMW"];
cars.remove(1);
                                    

The cars array now has the following values: "Tesla", "BMW".

What's next?

The next part discusses operators in Cuneiform.