Javascript code _ javascript tips-js tutorial

Source: Internet
Author: User
If an array is known, we want to use the specified method to operate the elements in the array one by one. For more information, see. The function is as follows:

The Code is as follows:


Foreach = function (obj, insp ){
If (obj = null & obj. constructor! = Array ){
Return [];
}
// Obj is the array to be processed. obj = null indicates that the object does not exist. obj. constructor! = Array indicates that the constructor of the object obj attribute is not an Array;
// The constructor attribute always points to the constructor that creates the current object. If both conditions are met, an empty array [] is returned.
// Learn more about the constructor attributes.
Var obj = [1, 2, 3, 4]; // equivalent to var obj = new Array (1, 2, 3, 4 );
Console. log (obj. constructor === Array); // returns true, indicating that the obj constructor is Array;
Var foo = function () {}; // equivalent to var foo = new Function ();
Console. log (foo. constructor === Function); // returns true, indicating that the foo constructor is a Function;
Var obj = new Foo (); // an obj object is instantiated by the constructor.
Console. log (obj. constructor = Foo); // return true, indicating that the obj constructor is Foo;


Bytes -----------------------------------------------------------------------------------------------------------
Var I = 0, len = obj. length, r = [];
While (I Var x = insp (obj [I], I );
If (x! = Null ){
R [r. length] = x;
}
I ++;
}
Return r;
};
// Traverses the array object obj. The insp parameter represents a method or function used to operate each element in obj. Two parameters are passed to the insp parameter. obj [I] represents each element value in the array. The insp parameter is assigned to x, and the x value is finally assigned to the array r.
Bytes -----------------------------------------------------------------------------------------------------------
For example, we want to traverse every element in the array and reference the foreach function.
Var testArray = [1, 2, 3, 4, 5, 1, 2, 'w'];
Foreach (testArray, function (I ){
Alert (I)
});
Bytes -----------------------------------------------------------------------------------------------------------
Bytes -----------------------------------------------------------------------------------------------------------
We use another function to delete the specified elements in the specified array;

The Code is as follows:


ArrayWithout = function (){
If (arguments. length <2 ){
// Arguments is a special object that represents a function parameter.
Return arguments. length = 1? Arguments [0]: null;
}
Var results = [];
Var aa = arguments [0];
// Assign the first parameter to array aa;
If (aa = null | aa. constructor! = Array ){
Return null;
// If aa does not exist or is not an array, null is returned;
}
If (arguments [1]. constructor = Array ){
// If the second parameter is an array, each element in the parameter array is deleted;
Var args = [];
Args [0] = aa;
// Aa = arguments [0] assigns aa to the array args as its first element value;
Foreach (arguments [1], function (v, I ){
// Reference the foreach function to operate each element in array arguments [1,
Args [I + 1] = v;
// V indicates each element value in the array arguments [1]. assign them to args [1], args [2]..., args [0] = arguments [0];
});
}
Else {
Args = arguments;
// If the second parameter is not an array, the parameter array is directly assigned 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 starts from 1, because args's first element, args [0], is arguments [0]. It is the original array to be processed, which is equivalent to array aa;
Compare the elements to be deleted with one element in the original array aa. If the elements are the same, the break jumps out of the loop. isWithout returns false, so the following results is not executed. push (aa [I]);
}
}
If (isWithout ){
Results. push (aa [I]);
// The so-called deletion principle is to compare the elements of the original array with the elements to be deleted, retain elements different from the elements to be deleted, and assign a new array results;
}
}
Return results;
// Returns the array with the specified Element deleted;
};
// Example of referencing 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:

The Code is as follows:


Foreach = function (obj, insp ){
If (obj = null & obj. constructor! = Array ){
Return [];
}
Var I = 0, len = obj. length, r = [];
While (I 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]

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.