JavaScript Array Powerful Splice method

Source: Internet
Author: User
Tags javascript array

Javascript There are always some so-called details that will make you dumbfounded.

A time ago, the project team added a need to require all display time in the system where the date format can be customized

what is the date format can be customized, in the usual, we see the time format is generally " YYYY-MM-DD "form, now to support the following six kinds:

YYYY-MM-DD Hh:mm:ss

MM-DD-YYYY Hh:mm:ss

DD-MM-YYYY Hh:mm:ss

YYYY/MM/DD Hh:mm:ss

MM/DD/YYYY Hh:mm:ss

DD/MM/YYYY Hh:mm:ss

The system also deliberately has a page to customize the format of the system, once set to refresh the page system has a module design time format of the display, all become the latest format.

simple support These are simple, there is a comparison of the pit father is a multi-page sign operation, one page to change the format, the other page does not refresh also have to keep the current page format ( SE specifications).

The current scenario is that the foreground is forwarded to the background before the request is made YYYY-MM-DD , then the logic in the background does not change, and the background returns the data to the current page format.

Now, there is a page to pass a time period string to the background query, for example:

04/16/2015 19:57:10~ 04/16/2015 21:57:10

It needs to be dealt with here.

var eventtime = "04/16/2015 19:57:10~ 04/16/2015 21:57:10"; Eventtime.split ("~");//["04/16/201519:57:10", "04/16/2015 21:57:10"]

the array is then traversed, and each element is then split into["04/16/2015","19:57:10"]and then convert the first half to the latest formatYYYY-MM-DD, replacing the value of the first seat, and then stitching it into a legitimate format.eventtimeto the background, by accident, by using an array ofSplice ()method; Suddenly discovered, its powerful function, share to everybody.

Now, let's talk about Most powerful splice () function w3school on its syntax, as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6B/8A/wKiom1Uvy5vzjCrDAAIwpTWDU7c577.jpg "title=" Splice.jpg "alt=" wkiom1uvy5vzjcrdaaiwptwdu7c577.jpg "/> below to give a few simple examples of implementation, adding and deleting the effect.

adding elements

var arrjxj = [1,2,3,4,5,6];undefinedarrjxj.splice (3,0,12,13,14); []arrjxj[1, 2, 3, 4, 5, 6]arrjxj.splice (3,1,15,16,17); [12]arrjxj[1, 2, 3, 15, 16, 17, 13, 14, 4, 5, 6]

you can see that if you want to add an effect to an element, the first argument is the one you want to replace, the second argument is 0; The third parameter replaces the position element;

Delete Element

Arrjxj.splice (3,3); [17]arrjxj[1, 2, 3, 4, 5, 6]arrjxj.splice (3,2); [14]arrjxj[1, 2, 3, 4, 5, 6]

As you can see, if you do not specify a third parameter, you can implement the deletion of the element at a particular location;

modifying elements

Arrjxj[1, 2, 3, 14]arrjxj.splice (3,2,20,21); [13]arrjxj[1, 2, 3, 20, 21, 14]

the number of the second parameter, like the third parameter bit, can be used as a replacement The results.

Of course, the above example actually changed, the array itself, which belongs to the deep copy , so you need to pay attention to it.


This article is from the "Shuizhongyue" blog, make sure to keep this source http://shuizhongyue.blog.51cto.com/7439523/1633661

JavaScript Array Powerful Splice method

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.