Examples are as follows:
Copy Code code as follows:
/**
* Delete array elements by value
*
* @param mixed value element values
* @returns Array
*/
Array.prototype.deleteValue = function (value) {
var i = 0;
for (i) {
if (this[i] = = value) break;
}
Return This.slice (0, I). Concat (This.slice (parseint (i, 10) + 1));
}
Example
var test = new Array (1,5,3,4,2);
Output 5
Console.log (test.length);
Delete an element with a value of 4
Test = Test.deletevalue (4);
Output [1, 5, 3, 2]
Console.log (test);
Output 4
Console.log (test.length);
/**
* Delete array elements by index
*
* @param int Index Element
* @returns Array
*/
Array.prototype.deleteIndex = function (index) {
Return This.slice (0, index). Concat (This.slice (parseint (index, 10) + 1);
}
Example
var test = new Array (1,5,3,4,2);
Output 5
Console.log (test.length);
Delete an element with an index of 1
Test = Test.deleteindex (1);
Output [1, 3, 4, 2]
Console.log (test);
Output 4
Console.log (test.length);