javascript - js如何将匹配到的数组元素删掉?
问题描述
var arr = [ { ServiceID: ’go-storage-127.0.0.1-8080-9090’, ServiceName: ’storage’, }, { ServiceID: ’System-xxx-192.168.0.111-8000-8000’, ServiceName: ’xxx’, }, { ServiceID: ’System-xxx2-192.168.0.111-8000-8000’, ServiceName: ’xxx2’, }, { ServiceID: ’System-xxx3-192.168.0.111-8000-8000’, ServiceName: ’xxx3’, }, {ServiceID: ’System2-xxx3-192.168.0.111-8000-8000’,ServiceName: ’xxx3’, }, {ServiceID: ’test-xxx3-192.168.0.111-8000-8000’,ServiceName: ’xxx3’,}];
将arr数组中ServiceID以test或者System开头的数组元素删掉 用删掉的方法总是没法讲匹配到的全删,哪位高手能帮个忙呢?谢谢!
问题解答
回答1:arr = arr.filter(item => !(/^test|^System/i.test(item.ServiceID)))
回答2:var startsWithArr = strArr => str => { return strArr.some(e => str.startsWith(e)); }var starts = startsWithArr([ ’test’, ’System-’]);var filterArr = arr => { arr.filter(e => !starts(e.ServiceID)); }回答3:
用Array.filter方法,将过滤后的数组赋值回arr;
arr = arr.filter(function(item) { return !(/^(test|System)/g.test(item.ServiceId || ’’));});
相关文章:
1. python 多进程 或者 多线程下如何高效的同步数据?2. windows-7 - win7下使用cmder,如何设置vim的tab为4个空格?3. web - nginx location 搜索算法问题!?4. media-query - 请教为何CSS3媒体查询语法不能生效?5. thinkPHP5中获取数据库数据后默认选中下拉框的值,传递到后台消失不见。有图有代码,希望有人帮忙6. python - 怎么进行服务器性能分析7. javascript - 关于js高级程序中的问题8. MySQL的SELECT...FOR UPDATE究竟起什么作用9. javascript - nodejs使用mongoose连接数据库,使用post提交表单在后台,后台处理后调用res.redirect()跳转界面无效?10. mysql - 要取出数据库中按某字段排序后的前10,20,30条数据要怎么做?

网公网安备