javascript - js如何將匹配到的數(shù)組元素刪掉?
問題描述
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數(shù)組中ServiceID以test或者System開頭的數(shù)組元素刪掉 用刪掉的方法總是沒法講匹配到的全刪,哪位高手能幫個忙呢?謝謝!
問題解答
回答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方法,將過濾后的數(shù)組賦值回arr;
arr = arr.filter(function(item) { return !(/^(test|System)/g.test(item.ServiceId || ’’));});
相關(guān)文章:
1. javascript - node.js promise沒用2. node.js - nodejs如何發(fā)送請求excel文件并下載3. 為什么我ping不通我的docker容器呢???4. golang - 用IDE看docker源碼時的小問題5. android 如何實現(xiàn)如圖中的鍵盤上的公式及edittext的內(nèi)容展示呢6. 算法 - python 給定一個正整數(shù)a和一個包含任意個正整數(shù)的 列表 b,求所有<=a 的加法組合7. java - 我在用Struts2上傳文件時,報以下錯誤怎么回事?8. mysql - 求SQL語句9. MySQL如何實現(xiàn)表中再嵌套一個表?10. PHP注冊功能
