How does I remove a particular element from an array in JavaScript?

Source: Internet
Author: User

9090down voteaccepted

Find index The of the array element you want to remove and then remove that index with splice .

The splice () method changes the contents of an array by removing existing elements and/or adding new elements.

var array = [2, 5, 9];console.log(array)var index = array.indexOf(5);if (index > -1) {  array.splice(index, 1);}// array = [2, 9]console.log(array);

The second parameter of are the number of splice elements to remove. Note that modifies the array in place and splice returns a new array containing the elements that has been removed.

From:https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript

How does I remove a particular element from an array in JavaScript?

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.