Table of Contents

Javascript - Array

About

array in Javascript can contain any type of data. Different types of values can be stored within the same array

Because arrays are special objects (as typeof implies), they can also have properties, including the automatically updated length property.

Property

Key

An array is a Object Derived Type (ie an object) , therefore the key of an array are the name of a property where the names are automatically set to a automatic increasing sequence of number if not set.

Proof:

let a = ["a","b","c"];
console.log("The type of an array is "+typeof(a));
a["foo"]="d";
console.log("The value of a are: ")
console.log( a)
console.log("But the length is (not 4) but "+a.length);

If you want a specific associative array implementation (ie a map), see the map data type article

Management

Initialization

Manually

var anEmptyArrays = []
var numberArrays = [1,2,3,4,5]
console.log(numberArrays)
var arrayWithTwoRows = [[1,2,3], [2,3,4]]
console.log(arrayWithTwoRows)

Generation

ArrayFrom

let integerArray = Array.from(Array(10), (element, index) => index+2);
console.log(integerArray);

Length

var numbers = [1,2,3,4,5]
console.log(numbers.length)

Type / is Array

The type of an array is an object. Luckily, to recognize a variable hosting an array, you can use the Array.isArray function.

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

console.log(typeof numbers);
console.log(Array.isArray(numbers));

It seems that the below expression should be true but it's false.

console.log(Array.isArray({__proto__: []})); 

Indexing

var arrayName = [1,2,3];
for (var i = 0; i < arrayName.length; i++) {
      console.log(arrayName[i]);
}

Merge

let array1 = [0,2];
let array2 = [4,2];
// delete the value of the second array found in the first
let array2WithoutArray1Values = array2.filter(element => array1.indexOf(element) < 0)
// merge by position
let array3 = array1.concat(array2WithoutArray1Values);
// result
console.log(array3)

Concatenate

var arr1 = [0, 2, 3];
var arr2 = [3, 4, 5];
arr1 = arr1.concat(arr2 );
console.log(arr1);
var arr1 = [0, 2, 3];
var arr2 = [3, 4, 5];
arr1 = [...arr1, ...arr2];
console.log(arr1);

Insert

Splice

arr.splice(index, deleting, item);

will:

myArray = ["Nico","Rixt","Melissa"]
console.log(myArray);
myArray.splice(2, 0, "Madelief");
console.log(myArray);

Spread

var parts = ['shoulders', 'knees']; 
var lyrics = ['head', ...parts, 'and', 'toes']; 
console.log(lyrics);

Push

Push inserts elements into an array one by one. In ES5 it's solved with .apply(): an unfriendly and verbose approach

// Syntax
array.push(item1, ..., itemN) 

Example: push into an existing array, without creating a new instance. with the ES5 apply

var parts = ['head', 'shoulders', 'knees']; 
var lyrics = ['and', 'toes']; 
Array.prototype.push.apply(parts, lyrics);  
console.log(parts); 

Remove

const index = array.indexOf(element);
    
if (index !== -1) {
    array.splice(index, 1);
}
newArray = array.filter(e => e !== element);

The set contains a delete function

myArray.length = 0;

Copy

var arr = [1, 2, 3];
var arr2 = [...arr]; // like arr.slice()
arr2.push(4); 

console.log("arr2 becomes "+arr2);
console.log("arr remains unaffected "+arr);

Slice

arr.slice()

Loop

Map

See Javascript - Map function (Functional programming)

For Of

var colors = [ "blue", "green" ];
console.log("color values are:");
for (const color of colors) {
    console.log("  "+color);
}

More for iteration, see the following page: Javascript - For Statement

For i

var colors = [ "blue", "green" ];
for (var i = 0; i < colors.length; i++) {
    console.log(i+" "+colors[i]);
}

More for iteration, see the following page: Javascript - For Statement

Equality

Both == and === object equality operator will simply check whether the references match, not anything about the underlying values.

var a = [1,2,3]; 
var b = [1,2,3]; 
var c = "1,2,3"; 
console.log( a == c ); // true 
console.log( b == c ); // true 
console.log( a == b ); // false

Contains

Example;

var a = [1,2,3]; 
console.log("2 is included in the array: "+ a.includes(2) ); 
console.log("4 is included in the array: "+ a.includes(4) ); 
console.log("2 is in the array: "+ (a.indexOf(2) !== -1) ); 
console.log("4 is in the array: "+ ( a.indexOf(4) !== -1) ); 

Sort

See How to sort an array of objects in Javascript?

To String

To Javascript - String

Join

let a = ["a","b","c"];
console.log(a.join("/"));