JavaScript method for deleting a specified value element from an array, javascript Array

Source: Internet
Author: User
Tags javascript array

JavaScript method for deleting a specified value element from an array, javascript Array

This example describes how JavaScript deletes a specified value element from an array. Share it with you for your reference. The specific analysis is as follows:

The following code uses two methods to delete Array elements. The first is to define a separate function, and the second is to define a removeByValue method for the Array object. The call is very simple.

Define the removeByValue function to delete an element.

Function removeByValue (arr, val) {for (var I = 0; I <arr. length; I ++) {if (arr [I] = val) {arr. splice (I, 1); break ;}} var somearray = ["mon", "tue", "wed", "thur"] removeByValue (somearray, "tue"); // somearray will now have "mon", "wed", "thur"

Adding corresponding methods to the array object makes it easier to call. You can directly call the removeByValue method of the array to delete the specified element.

Array. prototype. removeByValue = function (val) {for (var I = 0; I <this. length; I ++) {if (this [I] = val) {this. splice (I, 1); break ;}} var somearray = ["mon", "tue", "wed", "thur"] somearray. removeByValue ("tue"); // somearray will now have "mon", "wed", "thur"

I hope this article will help you design javascript programs.

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.