This article mainly introduces js operations on Array functions. The example shows how JavaScript can delete specified elements, deduplication, and delete specified subscript elements from arrays, for more information about js operations, see the following example. We will share this with you for your reference. The details are as follows:
1. Delete the specified element from the array.
/*** Reference instance 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 of the current object. If both conditions are met, an empty array [] is returned. // The following describes 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); // return true to indicate that the foo constructor is Function; var obj = new Foo (); // The constructor instantiates an obj object console. log (obj. constructor = Foo); // return true, indicating that the obj constructor is Foo; * // Delete the specified function del (val, arr) in the array {// check Test the parameter if (arr = null & arr. constructor! = Array) {return [];} var newarr = []; // save nonexistent values to the new Array for (var I = 0; I <arr. length; I ++) {if (arr [I]! = Val) newarr. push (arr [I]);} return newarr;} alert (del (2, [1, 2, 3, 4, 5, 2]);
2. Remove duplicate elements
/*** Remove repeated elements from the array and save the element value as the key of a new array. The key cannot be repeated, then you can set the number of variables */function unique (data) {data = data | []; var a ={}; len = data. length; for (var I = 0; I <len; I ++) {var v = data [I]; if (typeof (a [v]) = 'undefined') {a [v] = 1 ;}}; data. length = 0; for (var I in a) {data [data. length] = I;} return data;} alert (unique ([12, 12, 12, 34]);
3. Delete the element of the specified base object in the array.
/*** Delete the specified subscript element of the array ** the I value is always changing, and the n value is changed only when the if condition is true (it will increase sequentially) */Array. prototype. remove = function (dx) {if (isNaN (dx) | dx> this. length) {return false;} for (var I = 0, n = 0; I
I hope this article will help you design JavaScript programs.