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 - jQuery post()方法,里面的請求串可以轉(zhuǎn)換為GBK編碼么?可以的話怎樣轉(zhuǎn)換?2. javascript - 關(guān)于apply()與call()的問題3. css3 - 純css實現(xiàn)點擊特效4. javascript - avalon使用:duplex設(shè)置select默認(rèn)option的bug5. python - 在sqlalchemy中獲取剛插入的數(shù)據(jù)id?6. Python中使用超長的List導(dǎo)致內(nèi)存占用過大7. javascript - axios請求回來的數(shù)據(jù)組件無法進行綁定渲染8. 安全性測試 - nodejs中如何防m(xù)ySQL注入9. javascript - 有適合開發(fā)手機端Html5網(wǎng)頁小游戲的前端框架嗎?10. html5 - 請問現(xiàn)在主流的前端自動化構(gòu)建工具是哪個?
