In fact, after a good time of Python, I think the most I have to remember is not the python every time in writing functions or loops can be less than {} brackets such things (PS: actually also. Feel very refreshing, and began to write JS when always want to use XXX in range (): ... Kneeling. :( )
But I feel that the slicing of the string manipulation really makes me feel better than ever.
Imagine that we usually operate the most arrays and strings with the help of slicing, do not know how much time and trouble to save. And with slices you don't even have to remember many of the APIs that can be sliced instead.
Today, I was eating "learning JavaScript data structure and algorithms," inadvertently found that JS in fact, the rational use of
Array.prototype.splice ()
This guy, too, can easily achieve the effect of slicing, without having to memorize the tedious APIs.
For example, let me first declare an array of experiments to be used below.
var laplace = [0,1,2,3,4,5,6,7,8]
And then I'm going to delete the array inside the first element according to the slice should be
Laplace[1:]
and JavaScript can be implemented using the Unshift () method, or you can use the splice () method, similar to the slice operation, that I'm talking about
Laplace.splice (0,1);
Returns the first element that was deleted.
Splice () method The first parameter represents the starting position of the operand array, the second parameter represents the number of deleted elements, and the third parameter is the value to be added to the array.
Then if you want to delete the last parameter inside the array, you can use the
Laplace.splice (laplace.lenth-1,1);
You don't have to remember the same API as Shift () and unshift (). Does it feel a little convenient?
However, special attention should be paid here. The abstraction level of a python slice is much higher than the splice method, which can only be used in arrays. So please pay attention when using it.
How to use a python-like slice manipulation array in JavaScript