To delete the JavaScript code for a specified element in an array

Source: Internet
Author: User

The principle of deleting an array-specific element is simple, but it is simply traversing the array to find out if the given element is in, and if it is found, delete it, we will illustrate it by an application example below.

Arraywithout = function () {
if (Arguments.length < 2) {
Arguments is a special object that represents the parameters of a function.
return arguments.length = = 1? ARGUMENTS[0]: null;
}
var results = [];
var aa = arguments[0];
Assigning the first parameter to an array AA;
if (AA = = NULL | | aa.constructor!= Array) {
return null;
If AA does not exist or is not an array, then returns null;
}
if (Arguments[1].constructor = = array) {
If the second argument is an array, the corresponding element in the parameter array is deleted;
var args = [];
Args[0] = AA;
AA = Arguments[0] Assigns a AA to the array args as its first element value;
foreach (Arguments[1], function (V, i) {
The function foreach is referenced to operate on each element of the array arguments[1].
Args[i+1] = v;
V represents the values of each element in the array arguments[1], assigning them one by one to args[1],args[2] ..., and args[0]=arguments[0];
});
}
else{
args = arguments;
If the second argument is not an array, the parameter array is assigned directly to args;
}
for (var i = 0;i < aa.length i + +) {
var iswithout = true;
for (var j = 1; j < Args.length; J + +) {
if (aa[i] = = Args[j]) {
Iswithout = false;
Break
Remember that J is starting from 1, because the first element of args Args[0] is arguments[0], exactly the original array we are dealing with, equivalent to array AA;
Let each element of the deletion be compared to an element in the original array AA, and if the same, the break jumps out of the loop; Iswithout returns FALSE, so the following Results.push (Aa[i) is no longer executed;
}
}
if (iswithout) {
Results.push (Aa[i]);
The principle of deletion is to compare the elements of the original array with the elements to be deleted, to preserve elements that are not the same as the elements to be deleted and to assign a new array results;
}
}
return results;
Returns an array of the specified elements that have been deleted;
};
Examples of quoting Arraywithout
var testarray = [1,2,3,4,5,1,2, ' W '];
var result = Arraywithout (Testarray, 1, 3);
var result = Arraywithout (Testarray, [1, 4]);
Alert (Result)//[2,4,5,2]

In summary, the push is used here, and if you traverse the data and then compare and then delete it, the deletion specifies the array element.

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.