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. debian - docker依赖的aufs-tools源码哪里可以找到啊?2. html5 - node静态资源服务器设置了Cache-Control,但浏览器从来不走3043. docker start -a dockername 老是卡住,什么情况?4. 关于docker下的nginx压力测试5. docker安装后出现Cannot connect to the Docker daemon.6. docker镜像push报错7. docker内创建jenkins访问另一个容器下的服务器问题8. 为什么我ping不通我的docker容器呢???9. nignx - docker内nginx 80端口被占用10. dockerfile - 为什么docker容器启动不了?

网公网安备