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. node.js - nodejs+express+vue2. javascript - 我的站點貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?3. java - web端百度網盤的一個操作為什么要分兩次請求服務器, 有什么好處嗎4. 數據庫 - Mysql的存儲過程真的是個坑!求助下面的存儲過程哪里錯啦,實在是找不到哪里的問題了。5. python - 如何把152753這個字符串轉變成時間格式15:27:536. 使用mysql命令行連接遠程數據庫host跳轉7. javascript - onclick事件點擊不起作用8. css - C#與java開發Windows程序哪個好?9. javascript - webpack-dev-server和webpack沖突嗎10. python 字符串匹配問題

網公網安備