JavaScript Array
Views (2537)
Methods pop and push - manipulate with the end of array, are fast( O(1) )
Methods shift/unshift - manipulate with the starting of array , are slow (renumber whole array)
Method splice - it can delete elements and replace them.
It will remove a single element from an array. Array numbers shift to fill the gap.
splice is able to insert elements, just set deleteCount to 0.
Method slice - extract a portion of array using slice(begin[, end])
Sort
var program = ["Java", ".NET", "PHP"]
program.pop() // PHP removed
display(program) ; // ["Java", ".NET"]
alert("Now length is: "+program.length)
program.push("Oracle");
display(program) // ["Java", ".NET" , "Oracle"]
program.push("SQL Server", "JSON");
display(program) // ["Java", ".NET" , "Oracle", "SQL Server", "JSON"]
function display(program) {
for(var i=0; i<program.length; i++) {
alert("Hello, " + program[i]) }
}
program.pop() // PHP removed
display(program) ; // ["Java", ".NET"]
alert("Now length is: "+program.length)
program.push("Oracle");
display(program) // ["Java", ".NET" , "Oracle"]
program.push("SQL Server", "JSON");
display(program) // ["Java", ".NET" , "Oracle", "SQL Server", "JSON"]
function display(program) {
for(var i=0; i<program.length; i++) {
alert("Hello, " + program[i]) }
}
Methods shift/unshift - manipulate with the starting of array , are slow (renumber whole array)
program.shift() // Java removed
display(program) ; // [".NET" , "Oracle", "SQL Server", "JSON"]
program.unshift("PHP") // PHP added
display(program) // ["PHP, ".NET" , "Oracle", "SQL Server", "JSON"]
program.unshift("BizTalk", "SharePoint"); // added
display(program) // ["BizTalk", "SharePoint", "PHP, ".NET" , "Oracle", "SQL Server", "JSON"]
A delete operator removes key-value pairdisplay(program) ; // [".NET" , "Oracle", "SQL Server", "JSON"]
program.unshift("PHP") // PHP added
display(program) // ["PHP, ".NET" , "Oracle", "SQL Server", "JSON"]
program.unshift("BizTalk", "SharePoint"); // added
display(program) // ["BizTalk", "SharePoint", "PHP, ".NET" , "Oracle", "SQL Server", "JSON"]
var program = ["BizTalk", "SharePoint", "PHP", ".NET" , "Oracle", "SQL Server", "JSON"];
delete program[1] // ["BizTalk", undefined, "PHP", ".NET" , "Oracle", "SQL Server", "JSON"];
alert(program[1]) // undefined
delete program[1] // ["BizTalk", undefined, "PHP", ".NET" , "Oracle", "SQL Server", "JSON"];
alert(program[1]) // undefined
Method splice - it can delete elements and replace them.
It will remove a single element from an array. Array numbers shift to fill the gap.
var program = ["BizTalk", "SharePoint", "PHP"]
program.splice(1, 1) // remove 1 element starting at index 1 // ["BizTalk", "PHP"]
program.splice(0, 1) // remove 1 element starting at index 1 // ["SharePoint", "PHP"]
var program = ["BizTalk", "SharePoint", "PHP", ".NET" ];
program.splice(0, 3, "Oracle", "SQL Server") //[ "Oracle", "SQL Server", ".NET" ]
var removed = program.splice(0, 2) // array of removed element
program.splice(1, 1) // remove 1 element starting at index 1 // ["BizTalk", "PHP"]
program.splice(0, 1) // remove 1 element starting at index 1 // ["SharePoint", "PHP"]
var program = ["BizTalk", "SharePoint", "PHP", ".NET" ];
program.splice(0, 3, "Oracle", "SQL Server") //[ "Oracle", "SQL Server", ".NET" ]
var removed = program.splice(0, 2) // array of removed element
splice is able to insert elements, just set deleteCount to 0.
var program = ["BizTalk", "SharePoint", "PHP", ".NET" ];
program.splice(1, 0, "Oracle", "JSON") // ["BizTalk", "Oracle", "JSON", "SharePoint", "PHP", ".NET" ];
alert(program)
var program = ["BizTalk", "SharePoint", "PHP", ".NET" ];
program.splice(-1, 0, "Oracle", "JSON") // ["BizTalk", "SharePoint", "PHP", "Oracle", "JSON", ".NET" ];
alert(program)
// at element -1 (pre-last), delete 0 elements, then insert "Oracle", "JSON"
program.splice(1, 0, "Oracle", "JSON") // ["BizTalk", "Oracle", "JSON", "SharePoint", "PHP", ".NET" ];
alert(program)
var program = ["BizTalk", "SharePoint", "PHP", ".NET" ];
program.splice(-1, 0, "Oracle", "JSON") // ["BizTalk", "SharePoint", "PHP", "Oracle", "JSON", ".NET" ];
alert(program)
// at element -1 (pre-last), delete 0 elements, then insert "Oracle", "JSON"
Method slice - extract a portion of array using slice(begin[, end])
var program = ["BizTalk", "SharePoint", "PHP", ".NET" ];
var arrslice = program.slice(0,2) //"BizTalk", "SharePoint"] - take 2 elements from starting at 0
var arrslice = program.slice(1) //"BizTalk", "SharePoint"] - take all elements from starting at 1
var arrslice = program.slice(0,2) //"BizTalk", "SharePoint"] - take 2 elements from starting at 0
var arrslice = program.slice(1) //"BizTalk", "SharePoint"] - take all elements from starting at 1
Sort
var num = [ 1, 2, 15 ]
num.sort() // 1, 15, 2 because sort converts everything to string and uses lexicographical order by default.
function compare(a, b) {
if (a > b) return 1
else if (a < b) return -1
else return 0
}
var num = [ 1, 2, 15 ]
arr.sort(compare) // 1, 2, 15
num.sort() // 1, 15, 2 because sort converts everything to string and uses lexicographical order by default.
function compare(a, b) {
if (a > b) return 1
else if (a < b) return -1
else return 0
}
var num = [ 1, 2, 15 ]
arr.sort(compare) // 1, 2, 15
This was originally posted here.

Like
Report
*This post is locked for comments