本文介绍了从数组中删除所有与特定字符串匹配的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从数组中删除与特定字符串匹配的所有元素的最简单方法是什么?例如:
What is the easiest way to remove all elements from array that match specific string? For example:
array = [1,2,'deleted',4,5,'deleted',6,7];
我想从数组中删除所有'已删除'
.
I want to remove all 'deleted'
from the array.
推荐答案
只需使用 Array.prototype.filter() 函数,用于获取条件元素
Simply use the Array.prototype.filter() function for obtain elements of a condition
var array = [1,2,'deleted',4,5,'deleted',6,7];
var newarr = array.filter(function(a){return a !== 'deleted'})
更新:ES6语法
let array = [1,2,'deleted',4,5,'deleted',6,7]
let newarr = array.filter(a => a !== 'deleted')
这篇关于从数组中删除所有与特定字符串匹配的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!