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:

  • creating a minimal array
let a = ["a","b","c"];
  • The typeof the array variable
console.log("The type of an array is "+typeof(a));
  • Adding a property to the object array
a["foo"]="d";
console.log("The value of a are: ")
console.log( a)

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

Management

Initialization

Manually

  • Empty Array
var anEmptyArrays = []
  • Array with number
var numberArrays = [1,2,3,4,5]
console.log(numberArrays)
  • Array with two rows
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));

Indexing

  • Return the ith element. First element is on 0.
var arrayName = [1,2,3];
for (var i = 0; i < arrayName.length; i++) {
      console.log(arrayName[i]);
}

Merge

Concatenate

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

// Can also be done with concat -> Append all items from arr2 onto arr1
// arr1 = arr1.concat(arr2);

Insert

Splice

arr.splice(index, deleting, item);

will:

  • insert item
  • into arr
  • at the specified index
  • deleting items first
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

  • Splice. Mutable operation.
const index = array.indexOf(element);
    
if (index !== -1) {
    array.splice(index, 1);
}
  • Map. Immutable Operation (ie create a new array)
newArray = array.filter(e => e !== element);

The set contains a delete function

  • All (keeping the reference)
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

For

See also the 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;

  • The array
var a = [1,2,3]; 
  • includes() method determines whether an array includes a certain value
console.log("2 is included in the array: "+ a.includes(2) ); 
console.log("4 is included in the array: "+ a.includes(4) ); 
  • the indexOf function may be also used with a predicate (indexOf return -1 if the element is not present)
console.log("2 is in the array: "+ (a.indexOf(2) !== -1) ); 
console.log("4 is in the array: "+ ( a.indexOf(4) !== -1) ); 

To String

Join

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

Powered by ComboStrap