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ù)組元素刪掉 用刪掉的方法總是沒法講匹配到的全刪,哪位高手能幫個(gè)忙呢?謝謝!
問題解答
回答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. objective-c - ios百度地圖定位問題2. html5 - 如何解決bootstrap打開模態(tài)modal窗口引起頁面抖動(dòng)?3. javascript - 求助關(guān)于js正則問題4. javascript - node.js服務(wù)端渲染解疑5. javascript - 求助這種功能有什么好點(diǎn)的插件?6. html5 - rudy編譯sass的時(shí)候有中文報(bào)錯(cuò)7. html - css 如何添加這種邊框?8. 為何 localStorage、sessionStorage 屬于html5的范疇,但是為何 IE8卻支持?9. 微信開放平臺(tái) - Android調(diào)用微信分享不顯示10. javascript - 關(guān)于定時(shí)器 與 防止連續(xù)點(diǎn)擊 問題
