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. Java中main方法里面的参数一定要是String []args吗?2. font-family - 我引入CSS3自定义字体没有效果?3. css - 如何讓圖片像雲一樣的行為?4. javascript - node.js中stat() access() open() readFile()都能判断文件是否存在?5. html5 - 目前 公司App 嵌入H5页面 做个 手机支付功能 没有做过 所以 请求各位有经验的 给个思路6. javascript - js 多维数组的问题7. node.js - Nodejs微信开发,视频上传成功,但是不返回给用户8. css3 - CSS优先级问题9. html - 关于CSS实现border的0.5px设置?10. mysql - sql 左连接结果union右连接结果,导致重复性计算怎么解决?
![Java中main方法里面的参数一定要是String []args吗?](http://www.haobala.com/attached/image/18.jpg)
网公网安备