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. docker內(nèi)創(chuàng)建jenkins訪問另一個(gè)容器下的服務(wù)器問題2. 如何解決Centos下Docker服務(wù)啟動(dòng)無響應(yīng),且輸入docker命令無響應(yīng)?3. 我在centos容器里安裝docker,也就是在容器里安裝容器,報(bào)錯(cuò)了?4. css3 - 學(xué)習(xí)css構(gòu)建圖形時(shí),遇到一個(gè)很有意思的現(xiàn)象,具體代碼如下5. 極光推送 - Android app消息推送 百度 極光 個(gè)推 信鴿哪個(gè)好一些?6. javascript - js閉包作用域7. html5 - 百度echart官網(wǎng)下載的地圖json數(shù)據(jù)亂碼8. html - css 使用字體的時(shí)候,格式有什么特殊要求嗎?9. 微信開放平臺 - android 微信支付后點(diǎn)完成按鈕,后回調(diào)打開第三方頁面,屏幕閃動(dòng),求解決方法10. javascript - echart+百度地圖
