javascript - js如何將匹配到的數(shù)組元素刪掉?
問(wèn)題描述
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開(kāi)頭的數(shù)組元素刪掉 用刪掉的方法總是沒(méi)法講匹配到的全刪,哪位高手能幫個(gè)忙呢?謝謝!
問(wèn)題解答
回答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方法,將過(guò)濾后的數(shù)組賦值回arr;
arr = arr.filter(function(item) { return !(/^(test|System)/g.test(item.ServiceId || ’’));});
相關(guān)文章:
1. mysql - 如何減少使用或者不用LEFT JOIN查詢?2. html5 - H5 audio 微信端 在IOS上不能播放音樂(lè)3. python - 編碼問(wèn)題求助4. python - 我在使用pip install -r requirements.txt下載時(shí),為什么部分能下載,部分不能下載5. 視頻文件不能播放,怎么辦?6. mysql - 分庫(kù)分表、分區(qū)、讀寫分離 這些都是用在什么場(chǎng)景下 ,會(huì)帶來(lái)哪些效率或者其他方面的好處7. mysql - jdbc的問(wèn)題8. python - Scrapy存在內(nèi)存泄漏的問(wèn)題。9. Python爬蟲(chóng)如何爬取span和span中間的內(nèi)容并分別存入字典里?10. mysql - 千萬(wàn)級(jí)數(shù)據(jù)的表,添加unique約束,insert會(huì)不會(huì)很慢?
