Arrays

An array is a collection of elements that use the same name but are distinguished by an index value.
Arrays are untyped collections and are always zero based.
The individual items within an array are called elements.
JavaScript only has one-dimensional arrays but you can build arrays of arrays.
Arrays do not have fixed upper bounds
The numerical value used to access the individual elements is called the index.
The elements can be stored at non-continuous locations in the array


Different Dimensions

An array of individual items

var arSimple = ['apple', 'banana', 'orange', 'pear']; 
var myValue = arSimple[0];

An array of key-value objects

var arObjects = [ 
   {
      name: 'Richard',
      country: 'France',
      age: 32,
   },
   {
      name: 'Richard',
      country: 'France',
      age: 32,
   }
];
var myValue = arObjects[1].name

An array of arrays (multi-dimensional array / 2D array)

var arPairs = [ 
   ['name', 'Richard'],
   ['country', 'France'],
   ['age', 32],
];
var myValue = arPairs[0][2];


Different Data Types

The elements in an array can have any data type

var myArray = [ 
   "some text",
   "numbers",
   "objects",
   200,
   false
];

Default Values



An array can contain anything

var mycollection = ['a',10,/a/,{ } ];  
mycollection[0]
mycollection.length /number of items and not the last index position

var myArr = new Array() // ?? 

myArr.indexOf 
myArr.lastIndexOf

reducer function ?

var cellvalues = myRange.values.reduce(function (a,b) { return a.concat(b); } . [ ] ); 


var range = context.workbook.names.getItem("MyNamedRange").getRange(); 
range.values returns a 2D array
range.values.length // returns count of rows which is the first dimension
range.values[0].length // returns count of columns which the ?? Dimension


function add() { 
   var tempValue = 0;
   for (i=0; i < arguments.length; i++) {
      tempValue += arguments[i];
   }
   return tempValue;
}

var myValue = add(1,2,3,4);
alert(myValue);

Associative Arrays

This type of array lets you access the items in an array using a string lookup value.

var assArray = []; 
assArray["one"] = "myvalue";
assArray["two"] = "myvalue2";
alert(assArray["two"]);

Adding Properties

It is possible to add properties to an array

myArray2.myproperty = "Text"; 



© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopNext