Insert elements at the specified position in the JS array, and insert elements in the js Array

Source: Internet
Author: User

Insert elements at the specified position in the JS array, and insert elements in the js Array
Original article: Array: Insert an Item at a Specific Index with JavaScript
Original Article Date: January 1, July 24, 2014
Translated on: February 1, July 26, 2014
Translated by: Tie

Many array-related tasks sound simple, but they are not always the case, and developers often do not use them. Recently, I encountered the following requirement: insert an element to a specific index of an existing array. It sounds easy and common, but it takes some time to study it.

// Original array var array = ["one", "two", "four"]; // splice (position, numberOfItemsToRemove, item) // concatenation function (index location, number of elements to be deleted, element) array. splice (2, 0, "three"); array; // The current array looks like this ["one", "two", "three", "four"]


If you are not disgusted with the extension of native JavaScript, you can add this method to the Array prototype:
Array.prototype.insert = function (index, item) {  this.splice(index, 0, item);};

In this case, you can call:
Var nums = ["one", "two", "four"]; nums. insert (2, 'three '); // note the array index, [, 2 ..] array // ["one", "two", "three", "four"]

I have made some other changes to the array. You may have read the following:
  • Remove an Item From an Array: deletes an element From an Array.
  • Clone Arrays: array Clone
  • Empty Arrays: Empty array
  • Sort Arrays: array sorting
Arrays is very useful-processing some tasks in JavaScript is complicated ...... You must write more code (code-heavy) than you actually need ). For more convenience, please add this article to favorites or save these fragments to your toolbox!


Js deletes the element of a specified value (not a specified position) from the array.

If you do not use a third-party framework, a similar extension function can return the subscript of an element based on the specified value. You can only search for the element and then delete it.

<Script type = "text/javascript">
Array. prototype. indexOf = function (val ){
For (var I = 0; I <this. length; I ++ ){
If (this [I] = val) return I;
}
Return-1;
};
Array. prototype. remove = function (val ){
Var index = this. indexOf (val );
If (index>-1 ){
This. splice (index, 1 );
}
};

Var array = [1, 2, 3, 4, 5];
Array. remove (3 );

</Script>

Add element to js Array

Var a = []; // create an array a. push (1); // Add it to the last a. unshift (); // Add it to the first position
It can also be added with subscript.


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.