Discover splice function in javascript, include the articles, news, trends, analysis and practical advice about splice function in javascript on alibabacloud.com
Splice in JavaScript is mainly used to operate arrays in js, including deleting, adding, and replacing arrays. This article describes the detailed usage of the splice method in JavaScript. For more information, see splice in JavaScript. It is mainly
Objective
Today I revisit the JavaScript, see the array method, there are two of the more similar methods--splice and splice, look like, is more than one P, but the usage is quite different.
In use, you can reduce confusion by selecting an API
Detailed usage of splice in JavaScript, javascriptsplice
Splice in JavaScript is mainly used to operate arrays in js, including deleting, adding, and replacing arrays.
Note:This method will change the original array !.
1. Delete-used to delete an
Splice () method is a less useful method, but the function is really good, and in our coding, often need to splice () method, first introduce this method.The splice () method in JavaScript is a strong array method that has many uses.The splice ()
JS, delete an element in the array by valueArray.prototype.indexOf = function (val) {for (var i = 0; i if (this[i] = = val) return i;}return-1;};Array.prototype.remove = function (val) {var index = This.indexof (val);if (Index >-1) {This.splice
Copy codeThe Code is as follows:Array. prototype. ArrayInsertAfter = function (Num, obj){Var tempArr = new Array ();Var l = this. length;For (var I = 0; I {TempArr. push (this. shift ());}L = tempArr. length;For (var I = 0; I {This. push (tempArr.
During the interview, if the examiner asks you to use JavaScript to insert, delete, and,
During the interview, if the examiner asks you to use JavaScript to insert, delete, and replace array elements. If you do not know the method used by Array.
ArticleDirectory
Syntax
Return Value
Description
Example 1
Example 2
Example 3
Splice, please note that splice is not a split, although the two looks very similar but not a thing.
Splice and not split can be used for
// the original array var array = ["One", "one", "four"]; // Splice (position, Numberofitemstoremove, item) // stitching function (index position, number of elements to delete, Element)Array.splice (2, 0, "three"); array; // now the array is like
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.