JS deletes an element method in an array. js deletes an array element.

Source: Internet
Author: User

JS deletes an element method in an array. js deletes an array element.

Deletes an element specified by an array.

First, you can define a function for the JS array object to find the location of the specified Element in the array, that is, the index. The code is:

Array.prototype.indexOf = function(val) { for (var i = 0; i < this.length; i++) { if (this[i] == val) return i; } return -1; }; 

Then, use the built-in functions of the js array to delete this element by obtaining the index of this element:

Code:

Array.prototype.remove = function(val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } }; 

This constructs such a function. For example, I have an array:

var emp = ['abs','dsf','sdf','fd'] 

If we want to delete the 'fd ', we can use:

emp.remove('fd'); 

An item of the deleted Array

Splice (index, len, [item]) Note: This method changes the original array.

Splice has three parameters. It can also be used to replace, delete, or add one or more values in the array.

Index: array starts subscript len: length of replacement/deletion item: Value of replacement, item is null if the delete operation

For example, arr = ['A', 'B', 'C', 'D']

Delete

// Delete a value whose start subscript is 1 and its length is 1 (len sets 1. If it is 0, the array remains unchanged) var arr = ['A', 'B ', 'C', 'D']; arr. splice (1, 1); console. log (arr); // ['A', 'C', 'D']; // delete a value whose start subscript is 1 and its length is 2 (len is set to 2) var arr2 = ['A', 'B', 'C', 'D'] arr2.splice (1, 2); console. log (arr2); // ['A', 'D']

Replace

// Replace the START subscript with 1 and the value of 1 with 'ttt'. len sets 1 var arr = ['A', 'B', 'C ', 'D']; arr. splice (, 'ttt'); console. log (arr); // ['A', 'ttt', 'C', 'D'] var arr2 = ['A', 'B', 'C ', 'D']; arr2.splice (1, 2, 'ttt'); console. log (arr2); // ['A', 'ttt', 'D'] Replace the start subscript with 1, and the two values with a length of 2 with 'ttt ', len sets 1

Add ---- len is set to 0, and item is the added value.

Var arr = ['A', 'B', 'C', 'D']; arr. splice (, 'ttt'); console. log (arr); // ['A', 'ttt', 'B', 'C ', 'D'] indicates to add a 'ttt' <span style = "font-size: 14px; font-family: Arial, Helvetica, sans-serif; background-color: rgb (255,255,255); "> </span>

After deleting the elements in the array, the delete method sets the marked value to undefined, and the length of the array does not change.

Var arr = ['A', 'B', 'C', 'D']; delete arr [1]; arr; // ["a", undefined × 1, "c", "d"] two commas appear in the middle, the array length remains unchanged, and one item is undefined

The above JS method to delete an element in the array is all the content shared by the editor. I hope to give you a reference and support for the help house.

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.