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. javascript - 在靜態頁面上用load 引入的頁面文件問題?2. javascript - webpack打包后的bundlejs文件代碼不知道什么意思.3. android - RxJavar用什么操作符可以使數據每隔一段時間取出一個4. Android的webView如何實現網頁 錄音功能?5. Java游戲服務器開發和網站、app服務端的開發都差不多的嗎???實現的思路和方法6. css - 關于ul的布局7. html - 哪些情況下float會失效?8. javascript - vue組件通過eventBus通信時,報錯a.$on is not a function9. java - oracle對漢字字段按照拼音排序的函數和sql語句是什么?10. css - 如何使用 vue transition 實現 ios 按鈕一樣的平滑切換效果
