JavaScript - Array operations
var myArray = [];
//push - Adds new element to the end of the array
for (var i = 0; i < 6; i++) {
myArray.push(i);
}
document.write("Array elements are : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
document.write("Array length is : " + myArray.length + "<br/> <br/>");
//pop - removes last element of the array.
myArray.pop();
document.write("Array elements after pop operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
document.write("Array length is : " + myArray.length + "<br/> <br/>");
//Shift operation - Remove the first item of an array
myArray.shift()
document.write("<br/>Array elements after shift operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
document.write("<br/>");
//Unshift operation - Add new items to the beginning of an array
myArray.unshift(100, 101);
document.write("Array elements after unshift operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
//Reverse operation
myArray.reverse();
document.write("Array elements after Reverse operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
//Sort Operation
//Descending order --- myArray.sort(function (a, b) { return a - b;}).reverse();
myArray.sort(function (a, b) { return a - b;});
document.write("Array elements after Sort operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
//Splice operation
myArray.splice(2, 2);
document.write("Array elements after Splice operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
myArray.splice(1,0,5,10);
document.write("Array elements after Splice operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
var myArray = [];
//push - Adds new element to the end of the array
for (var i = 0; i < 6; i++) {
myArray.push(i);
}
document.write("Array elements are : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
document.write("Array length is : " + myArray.length + "<br/> <br/>");
//pop - removes last element of the array.
myArray.pop();
document.write("Array elements after pop operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
document.write("Array length is : " + myArray.length + "<br/> <br/>");
//Shift operation - Remove the first item of an array
myArray.shift()
document.write("<br/>Array elements after shift operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
document.write("<br/>");
//Unshift operation - Add new items to the beginning of an array
myArray.unshift(100, 101);
document.write("Array elements after unshift operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
//Reverse operation
myArray.reverse();
document.write("Array elements after Reverse operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
//Sort Operation
//Descending order --- myArray.sort(function (a, b) { return a - b;}).reverse();
myArray.sort(function (a, b) { return a - b;});
document.write("Array elements after Sort operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
//Splice operation
myArray.splice(2, 2);
document.write("Array elements after Splice operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
myArray.splice(1,0,5,10);
document.write("Array elements after Splice operation : <br/>");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br/>");
}
No comments:
Post a Comment