Home Web Front-end JS Tutorial Code to delete specified elements in specified array in javascript_javascript skills

Code to delete specified elements in specified array in javascript_javascript skills

May 16, 2016 pm 06:10 PM
element

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]

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In JavaFX, what are the different path elements? In JavaFX, what are the different path elements? Aug 28, 2023 pm 12:53 PM

The javafx.scene.shape package provides some classes with which you can draw various 2D shapes, but these are just primitive shapes like lines, circles, polygons and ellipses etc... So if you want to draw complex For custom shapes, you need to use the Path class. Path class Path class You can draw custom paths using this geometric outline that represents a shape. To draw custom paths, JavaFX provides various path elements, all of which are available as classes in the javafx.scene.shape package. LineTo - This class represents the path element line. It helps you draw a straight line from the current coordinates to the specified (new) coordinates. HlineTo - This is the table

C++ program: add an element to an array C++ program: add an element to an array Aug 25, 2023 pm 10:29 PM

An array is a linear sequential data structure used to hold homogeneous data in contiguous memory locations. Like other data structures, arrays must have the ability to insert, delete, traverse, and update elements in some efficient way. In C++, our arrays are static. C++ also provides some dynamic array structures. For a static array, Z elements may be stored in the array. So far we have n elements. In this article, we will learn how to insert elements at the end of an array (also known as appending elements) in C++. Understand the concept through examples. The usage of ‘this’ keyword is as follows: GivenarrayA=[10,14,65,85,96,12,35,74,69]Afterin

CSS transition effect: how to achieve the sliding effect of elements CSS transition effect: how to achieve the sliding effect of elements Nov 21, 2023 pm 01:16 PM

CSS transition effect: How to achieve the sliding effect of elements Introduction: In web design, the dynamic effect of elements can improve the user experience, among which the sliding effect is a common and popular transition effect. Through the transition property of CSS, we can easily achieve the sliding animation effect of elements. This article will introduce how to use CSS transition properties to achieve the sliding effect of elements, and provide specific code examples to help readers better understand and apply. 1. Introduction to CSS transition attribute transition CSS transition attribute tra

CSS transformation: how to achieve the rotation effect of elements CSS transformation: how to achieve the rotation effect of elements Nov 21, 2023 pm 06:36 PM

CSS transformation: How to achieve the rotation effect of elements requires specific code examples. In web design, animation effects are one of the important ways to improve user experience and attract user attention, and rotation animation is one of the more classic ones. In CSS, you can use the "transform" attribute to achieve various deformation effects of elements, including rotation. This article will introduce in detail how to use CSS "transform" to achieve the rotation effect of elements, and provide specific code examples. 1. How to use CSS’s “transf

What elements are not supported by html5 What elements are not supported by html5 Aug 11, 2023 pm 01:25 PM

Elements that HTML5 does not support are purely expressive elements, frame-based elements, application elements, replaceable elements and old form elements. Detailed introduction: 1. Purely expressive elements, such as font, center, s, u, etc., these elements are usually used to control text style and layout; 2. Frame-based elements, such as frame, frameset and noframes, these elements are used in In the past, it was used to create web page layouts and split windows; 3. Application-related elements, such as applet, isinde, etc.

How to remove elements with jquery How to remove elements with jquery Feb 17, 2023 am 09:49 AM

How to remove elements with jquery: 1. Delete the selected element and its sub-elements through the jQuery remove() method. The syntax is "$("#div1").remove();"; 2. Delete through the jQuery empty() method. The child elements of the selected element, the syntax is "$("#div1").empty();".

How to use CSS to achieve an element's transparency gradient effect How to use CSS to achieve an element's transparency gradient effect Nov 21, 2023 pm 01:38 PM

How to use CSS to achieve the transparency gradient effect of elements In web development, adding transition effects to web page elements is one of the important means to improve user experience. The gradient effect of transparency can not only make the page smoother, but also highlight the key content of the element. This article will introduce how to use CSS to achieve the transparency gradient effect of elements and provide specific code examples. Using the CSS transition attribute To achieve the transparency gradient effect of an element, we need to use the CSS transition attribute. t

How to implement a layout with a fixed navigation menu using HTML and CSS How to implement a layout with a fixed navigation menu using HTML and CSS Oct 26, 2023 am 11:02 AM

How to use HTML and CSS to implement a layout with a fixed navigation menu. In modern web design, fixed navigation menus are one of the common layouts. It can keep the navigation menu always at the top or side of the page, allowing users to browse web content conveniently. This article will introduce how to use HTML and CSS to implement a layout with a fixed navigation menu, and provide specific code examples. First, you need to create an HTML structure to present the content of the web page and the navigation menu. Here is a simple example

See all articles