Scripter Javascript Tutorial
javascriptmusiclogicscripterbookexcerpttutorial46 Delete Data from an Array
The pop()
function returns the last value in the array
and returns it.
var scale = [69, 71, 72];
var last = scale.pop();
// last = 72
// scale = [69, 71]
In the above example, the last value in the array was retrieved and then removed from the array.
The shift()
function removes the first element from the
array and returns it.
var scale = [60, 62, 64];
var first = scale.shift();
// first = 60
// scale = [62, 64]
The splice()
function removes an element from the array
at any location and returns it in an array.
var array = [60, 62, 64, 65, 67, 69, 71];
var value = array.splice( 3 , 1 );
// value = [65]
// array = [60, 62, 64, 67, 69, 71]
In the above example, the 4th value in the array is retrieved and then removed from the array.