Basic Usage
1st param – what index of the array to start
2nd param – how many elements to remove
1 |
var result = test.splice(1,2); |
in firebug console, we first see what test is. Which gives us the result of the full array. Then we do test.splice(1,2), which means at index 1, we remove 2 elements. This returns us “two” and “three”, meaning we removed “two” and “three.
Then we see the results of test again to ensure that “two” and “three” are removed from the original array.
> test
Array [ “one”, “two”, “three”, “four”, “five”, “six” ]
> test.splice(1,2)
Array [ “two”, “three” ]
> test
Array [ “one”, “four”, “five”, “six” ]
1 2 3 4 5 |
//1st param - what index of the array to start //2nd param - number of elements to remove //3rd param - string to add in place var result2 = test.splice(10, 2, "replacement"); |
in firebug console, we start at index 2, which would be “ten”.
We remove 2 elements, which would be the “ten”, and “six”. The removed elements are returned.
We then add the word “nine”.
> test
Array [ “one”, “four”, “ten”, “six” ]
> test.splice(2,2,”nine”)
Array [ “ten”, “six” ]
> test
Array [ “one”, “four”, “nine” ]
Using splice in List implmentation
We first make a List class default constructor. we initialize member variables. Then we assign function definitions append, remove, length, insert, clear, and toString.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function List() { this.listSize = 0; this.pos = 0; //position is current at 0 this.dataStore = []; //data store is empty //this.clear = clear; //function clear should clear everything this.find = find; //functind this.toString = toString; this.append = append; //functional variable points to func def this.remove = remove; this.length = length; this.insert = insert; this.clear = clear; |
delete the array of elements. then reassign to empty array.
1 2 3 4 5 6 7 |
function clear() { //remove from heap delete this.dataStore; this.dataStore = []; this.listSize = pos = 0; console.log("MyList.js - (clear), MyList object cleared"); } |
1 2 3 4 |
//adds an element to our dataStore array function append(element) { this.dataStore[this.listSize++] = element; } |
We loop through all the elements of our dataStore array, and if the elements match, we simply return the index.
1 2 3 4 5 6 7 8 9 |
function find(element) { for (var i = 0; i < this.dataStore.length; ++i) { if (this.dataStore[i] == element) { console.log("MyList.js - (find), found element " + element + ", at index " + i); return i; } } return -1; } |
We first get the index of where we want to remove the element.
Then when we have the index, we use the index as parameter for the splice index to know where to start. The 1 at the 2nd parameter of the splice, means to remove 1 element at that index.
1 2 3 4 5 6 7 8 9 10 11 |
//finds the index of the element //then splices the element function remove(element) { var foundAt = this.find(element); if (foundAt > -1) { this.dataStore.splice(foundAt,1); --this.listSize; return true; } return false; } |
We insert at given position pos, removing 0 elements, and insert the element to be inserted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//insert element into position function insert(element, pos) { if(this.listSize > 0) { this.dataStore.splice( pos, //start at what position 0, //remove 0 elements. element); //the element to replace or in case of splice, insert this.listSize++; console.log("MyList.js - (insert), element " + element + " inserted at position: " + pos); return true; } console.log("MyList.js - (insert), Warning: did not insert. The array does not exist."); return false; } |