1 Javascript - Shift , Slice, Splice Sun May 20, 2012 4:33 am
Mr.Joker
Mr. WebArtz
Hello WebArtz members, I am Mr.Joker and in this tutorial I will show you how to use methods as Shift, Slice and Splice. Its not difficult because you allready know how methods work allready. Here we go:
1. Shift : With shift method you remove first parameter form array. Here si one example:
Lets explain code a bit. We have here one Array with parameters red, blue and green. In next row I say :
Array_name.method()
Like that, i just removed the first element or parameter of Array. In last code line i just show the result.
2. Slice : With slice method you can select how many elements of array will be printed on screen. I can say it will be printed first two, or, second till end and so on with combinations. Example:
Here we have one Array with 5 elements. I created another variable caled "x" and inside it i said that Array color will be sliced from begining till element no.3 in Array. So this mean that document will print out Only first 3 elements of Array, but not the 4 as last parameter say. So if its 3 last that doesent mean it will be counted, its not counting that one.
3. Splice: With splice you can remove all other items and then print only the one you picked.
Code is quite simple. Array contain 5 elements. When you type color.splice(1,2) that mean that first two elements will be printed and all other will not.
Thats all for now, If you have any questions you can ask me
1. Shift : With shift method you remove first parameter form array. Here si one example:
- Code:
<script type="text/javascript">
var color = new Array("red","blue","green")
color.shift()
document.write(color);
</script>
Lets explain code a bit. We have here one Array with parameters red, blue and green. In next row I say :
Array_name.method()
Like that, i just removed the first element or parameter of Array. In last code line i just show the result.
2. Slice : With slice method you can select how many elements of array will be printed on screen. I can say it will be printed first two, or, second till end and so on with combinations. Example:
- Code:
<script type="text/javascript">
var color = new Array("red","blue","green","orange","yellow")
var x = color.slice(0,3)
document.write(x);
</script>
Here we have one Array with 5 elements. I created another variable caled "x" and inside it i said that Array color will be sliced from begining till element no.3 in Array. So this mean that document will print out Only first 3 elements of Array, but not the 4 as last parameter say. So if its 3 last that doesent mean it will be counted, its not counting that one.
3. Splice: With splice you can remove all other items and then print only the one you picked.
- Code:
<script type="text/javascript">
var color = new Array("red","blue","green","orange","yellow")
var x = color.splice(1,2)
document.write(x);
</script>
Code is quite simple. Array contain 5 elements. When you type color.splice(1,2) that mean that first two elements will be printed and all other will not.
Thats all for now, If you have any questions you can ask me