Javascript - Array

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > ECMAScript - Grammar Standard

1 - About

Arrays 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.

Advertising

3 - Management

3.1 - Initialization

  • Array

  • 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)

3.2 - Length

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

Advertising

3.3 - Type

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));

3.4 - 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]);
}

3.5 - 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);

3.6 - Insert

3.6.1 - 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);

Advertising

3.6.2 - Spread

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

3.6.3 - 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); 

3.7 - 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;

3.8 - 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);

3.9 - Slice

arr.slice()

3.10 - Loop

3.10.1 - Map

3.10.2 - For

var colors = [ "blue", "green" ];
  • Color is not the array value but the index
console.log("color value are:");
for (color in colors) {
    console.log("  "+color);
}
  • Using this index, when can access the value
console.log("\ncolors[color] values are:");
for (color in colors) {
    console.log("  "+colors[color]);
}
See also the for statement

3.11 - 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

web/javascript/es/array.txt · Last modified: 2019/09/01 14:06 by gerardnico