How to use a python-like slice manipulation array in JavaScript

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.