Skip to content

Instantly share code, notes, and snippets.

@wangwen1220
Last active January 4, 2016 13:29
Show Gist options
  • Select an option

  • Save wangwen1220/8628009 to your computer and use it in GitHub Desktop.

Select an option

Save wangwen1220/8628009 to your computer and use it in GitHub Desktop.
JS: 根据元素的值移除数组元素中的值 | removeByValue
// 根据元素的值移除数组元素中的值
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