formations/javascript/base/tableaux-1.md

677 B

.cadoles-slide-title[Tableaux (1/2)]

.cadoles-clearfix[ Différentes notations ]

var arr = [1, 2, 3]; // A préférer
arr = new Array(1, 2, 3);

// Attention !
arr = new Array(3);   // -> [undefined, undefined, undefined]

Manipulation d'un tableau

var arr = [1, 2, 3];

// Propriétés
arr.length          // -> 3
arr[0]              // -> 1

// Méthodes (liste non exhaustive)
arr.push(4);        // -> [1, 2, 3, 4]
arr.pop();          // -> [1, 2, 3]
arr.unshift(0);     // -> [0, 1, 2, 3]
arr.shift();        // -> [1, 2, 3]
arr.indexOf(2)      // -> 1
arr = [2, 3, 1];
arr.sort();         // -> [1, 2, 3]
arr.join(', ');     // -> '1, 2, 3'