JavaScript deletes the specified object from an array

Source: Internet
Author: User

Returns the subscript of an object in an array:

_arr represents an array of arrays that contains many objects such as:

_obj represents an Array object
function GetIndex (_arr,_obj) {            var len = _arr.length;              for (var i = 0; i < len; i++)            {                if(_arr[i] = = _obj                )                    {return  parseint (i);}            }             return -1;        };

Delete an object in an array: Returns the remaining array after deletion

/*delete an object in an array _arr: Array _obj: object to be deleted*/functionremoveaaary (_arr, _obj) {varLength =_arr.length;  for(vari = 0; i < length; i++) {        if(_arr[i] = =_obj) {            if(i = = 0) {_arr.shift ();//Delete and return the first element of the array                return_arr; }            Else if(i = = Length-1) {_arr.pop (); //Delete and return the last element of the array                return_arr; }            Else{_arr.splice (i,1);//Delete the element labeled I                return_arr; }        }    }}

JavaScript deletes the object specified in an array

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.