Home > Web Front-end > JS Tutorial > body text

Code to delete specified elements in specified array in javascript_javascript skills

WBOY
Release: 2016-05-16 18:10:57
Original
862 people have browsed it

The function is as follows:

Copy code 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 means that the object does not exist yet; obj.constructor != Array indicates that the constructor of the properties of object obj 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 [] will be returned;
// Let’s learn more about the constructor attribute.
var obj= [1, 2, 3, 4]; // Equivalent to var obj= new Array(1, 2, 3, 4);
console.log(obj.constructor === Array ); // Return true indicating that the constructor of obj is Array;
var foo= function() { }; // Equivalent to var foo = new Function();
console.log(foo.constructor = == Function); // Return true indicating that the constructor of foo is Function;
var obj = new Foo(); // Instantiate an obj object by the constructor
console.log(obj.constructor == = Foo); // Return true indicating that the constructor of obj is Foo;

------------------------- -------------------------------------------------- --------------------------------
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;
};
//Traverse the array object obj. The parameter insp represents a method or function, which is used to Operate on each element. Two parameters are passed to the parameter insp, obj[i] represents each element value in the array; the function parameter insp is assigned to x, and finally the x value is assigned to the array r.
-------------------------------------------------- -------------------------------------------------- ----------
For example, if we want to traverse each element in the array, reference 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 The code is as follows:

ArrayWithout = function(){
if (arguments.length < 2) {
// Arguments are special objects that represent the parameters of the function.
return arguments.length == 1 ? arguments[0] : null;
 }
var results = [];
var aa = arguments[0];
//Assign the first parameter to the array aa;
if (aa === null || aa.constructor != Array) {
Return null;
//If aa does not exist or is not an array, return null;
}
if(arguments [1].constructor == Array){
// If the second parameter is an array, delete each corresponding element in the parameter array;
 var args = [];
 args[ 0] = aa;
//aa = arguments[0] Assign aa to the array args as its first element value;
 foreach(arguments[1], function(v, i){
//Referenced the function foreach to operate on each element in the array arguments[1],
  args[i 1] = v;
// v represents the value of each element in the array arguments[1] , assign them one by one to args[1], args[2]..., and args[0]=arguments[0];
 });
  }
else{
  args = arguments;
//If the second parameter is not an array, directly assign the parameter array to args;
 }
 for(var i = 0;i < aa.length; i ){
 var isWithout = true; ;
   break;
// Remember that j starts from 1, because the first element of args args[0] is arguments[0], which is the original array we want to process, which is equivalent to the array aa;
Compare each element to be deleted with an element in the original array aa one after another. If they are the same, break will jump out of the loop; isWithout returns false, so the following results.push(aa[i]) will no longer be executed;
   }
  }
 if (isWithout) {
  results.push(aa[i]); Compare, retain elements that are different from the elements to be deleted and assign them to a new array results;
  }
 }
 return results;
//Return the array in which the specified element has been deleted;
};
// Example of citing 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
The code is as follows: foreach = function (obj, insp) { if(obj == null && obj.constructor != Array){
return [];
}
var i=0, len = obj.length, r = [];
while(ivar 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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template