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. css3 - 微信前端页面遇到的transition过渡动画的bug3. css - input间的间距和文字上下居中4. javascript - router.push无效5. javascript - postcss-loader在webpack2的使用.6. javascript - 为什么var obj = {}创建对象的方法里面不能用this.xxx来声明属性 ?7. css3 - 微信小程序如何把radio改成2个选择按钮的样式8. javascript - Storage中removeItem在什么情况下使用9. selenium-selenium-webdriver - python 将当前目录加入到 环境变量10. javascript - 浏览器回退,如何保证js对dom的操作保存下来

网公网安备