Last active
January 4, 2016 13:29
-
-
Save wangwen1220/8628009 to your computer and use it in GitHub Desktop.
JS: 根据元素的值移除数组元素中的值 | removeByValue
This file contains hidden or 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
| // 根据元素的值移除数组元素中的值 | |
| function removeByValue(arr, val) { | |
| for (var i = 0; i < arr.length; i++) { | |
| if (arr[i] == val) { | |
| arr.splice(i, 1); | |
| break; | |
| } | |
| } | |
| } | |
| // 更好的方式是使用prototype的方法去实现 | |
| Array.prototype.removeByValue = function(val) { | |
| for (var i = 0; i < this.length; i++) { | |
| if (this[i] == val) { | |
| this.splice(i, 1); | |
| break; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment