Splice () method in Javascript implements overlay array

Source: Internet
Author: User

Requirements: Use array arr2 to overwrite all elements in array arr1 when encapsulating Plug-ins

Definitions and usage

The splice () method inserts, deletes, or replaces the elements of an array.

Grammar

Arrayobject.splice (index,howmany,element1,....., elementx)

Example 1
In this case, we'll create a new array and add an element to it:

The code is as follows Copy Code

<script type= "Text/javascript" >

var arr = new Array (6)
Arr[0] = "George"
ARR[1] = "John"
ARR[2] = "Thomas"
ARR[3] = "James"
ARR[4] = "Adrew"
ARR[5] = "Martin"

document.write (arr + "<br/>")
Arr.splice (2,0, "William")
document.write (arr + "<br/>")

</script> output:

George,john,thomas,james,adrew,martin
George,john,william,thomas,james,adrew,martin

All right, it's just a brief introduction to this source, so let's introduce the splice () method to implement overlay arrays

Instance source code

  code is as follows copy code

<div id= "Demo" ></div>
<script type= "Text/javascript"
var arr1 =["element 1", "Element 2";
var arr2 =["arr2-element 1 "," arr2-element 2 "];
Arr1.splice (0,ARR1.LENGTH,ARR2);
document.getElementById ("Demo"). Innerhtml=arr1;
</script>

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.