Created
June 2, 2014 22:15
-
-
Save bennadel/4a9a85c33e1bc00d67fa to your computer and use it in GitHub Desktop.
Spliced - A Version Of Splice() That Returns The Original Array In JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title> | |
Spliced in Array Prototype To Return Original Array Reference | |
</title> | |
</head> | |
<body> | |
<h1> | |
Spliced in Array Prototype To Return Original Array Reference | |
</h1> | |
<script type="text/javascript"> | |
// This does the same exact thing as .splice(); but, it returns the original | |
// array reference rather than the collection of items that were deleted. | |
Array.prototype.spliced = function() { | |
// Returns the array of values deleted from array. | |
Array.prototype.splice.apply( this, arguments ); | |
// Return current (mutated) array array reference. | |
return( this ); | |
}; | |
// -------------------------------------------------- // | |
// -------------------------------------------------- // | |
// Logs the value, "0.a.b.1.2.3.4.x.y.5" | |
console.log( | |
"0.1.2.3.4.5" | |
.split( "." ) | |
.spliced( 1, 0, "a", "b" ) | |
.spliced( -1, 0, "x", "y" ) | |
.join( "." ) | |
); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! It helped me out!