Code _javascript tips for removing the specified elements from the specified array in JavaScript

Source: Internet
Author: User
The functions are as follows:
Copy Code code as follows:

foreach = function (obj, Insp) {
if (obj== null && obj.constructor!= Array) {
return [];
}
obj is the array to process, Obj==null indicates that the object does not exist; Obj.constructor!= array represents the property of the object obj is not an array;
The constructor property always points to the constructor that creates the current object. Two conditions are satisfied, an empty array is returned [];
The constructor property is further understood below.
var obj= [1, 2, 3, 4]; Equivalent to Var obj= new Array (1, 2, 3, 4);
Console.log (Obj.constructor = = Array); Returns true to indicate that the constructor for obj is array;
var foo= function () {}; Equivalent to var foo = new Function ();
Console.log (Foo.constructor = = Function); Returns true to indicate that the constructor of Foo is a function;
var obj = new Foo (); Instantiating an Obj object by a constructor
Console.log (Obj.constructor = = = Foo); Returns true to indicate that the constructor for obj is foo;

-----------------------------------------------------------------------------------------------------------
var i=0, Len = obj.length, r = [];
while (I<len) {
var x = Insp (Obj[i], i);
if (x!== null) {
R[r.length] = x;
}
i++;
}
return R;
};
The array object obj is traversed, and the parameter Insp represents a method or function that is used to manipulate each element in obj. Passed two parameters to the parameter Insp, Obj[i] represents each element value in the array, and assigns the function argument Insp to X, which eventually assigns the X value to the array R.
-----------------------------------------------------------------------------------------------------------
For example, we're going to enumerate every element in the group, referencing the foreach function.
var testarray = [1,2,3,4,5,1,2, ' W '];
foreach (Testarray, function (i) {
Alert (i)
});
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
We use another function to delete the specified element in the specified array;
Copy Code code as follows:

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]

The source code is as follows:
Copy Code code as follows:

foreach = function (obj, Insp) {
if (obj = = null && obj.constructor!= Array) {
return [];
}
var i=0, Len = obj.length, r = [];
while (I<len) {
var x = Insp (Obj[i], i);
if (x!== null) {
R[r.length] = x;
}
i++;
}
return R;
};
Arraywithout = function () {
if (Arguments.length < 2) {
return arguments.length = = 1? ARGUMENTS[0]: null;
}
var results = [];
var aa = arguments[0];
if (AA = = NULL | | aa.constructor!= Array) {
return null;
}
if (Arguments[1].constructor = = Array) {
var args = [];
Args[0] = AA;
foreach (Arguments[1], function (V, i) {
Args[i+1] = v;
});
}
else{
args = arguments;
}
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
}
}
if (iswithout) {
Results.push (Aa[i]);
}
}
return results;
};
var testarray = [1,2,3,4,5,1,2];
foreach (Testarray, function (i) {
Alert (i)
})
var result = Arraywithout (Testarray, 1, 3);
var result = Arraywithout (Testarray, [1, 3]);
Alert (Result)//[2,4,5,2]
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.