JavaScript uses loops and splits to replace and delete element instances, while javascript instances

Source: Internet
Author: User

JavaScript uses loops and splits to replace and delete element instances, while javascript instances

Elements can be replaced and deleted using loops and splits. It has been tested to be simple but practical, and is especially suitable for new users.

<! DOCTYPE> 

Ie8 does not support indexOf or splice


JavaScript element replacement

The following line has a problem:
For (I = 0; I <count; I ++)

Modify this line as follows:
For (I = count-1; I> = 0; I --)

Don't be confused. After modification, the test will find that it is correct. Because your original statement is equivalent to deleting input, When you modify two, the I loop value has exceeded the valid range.

In javascript, how does one Delete the element of a value in an array?

Test = newArray (); Array. prototype. removeByValue = function (val) {for (vari = 0; I <this. length; I ++) {if (this [I] = val) {this. splice (I, 1); break ;}} test [0] = 'apple'; test [1] = 'ball'; test [2] = 'cat '; test [3] = 'Dog'; alert ("Array before removing elements:" + test); test. removeByValue ('cat'); alert ("Array after removing elements:" + test );
Reference: hi.baidu.com/...1.html

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.