您的位置:首頁技術文章
文章詳情頁

javascript - js如何將匹配到的數組元素刪掉?

瀏覽:84日期:2023-08-31 17:30:00

問題描述

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數組中ServiceID以test或者System開頭的數組元素刪掉 用刪掉的方法總是沒法講匹配到的全刪,哪位高手能幫個忙呢?謝謝!

問題解答

回答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方法,將過濾后的數組賦值回arr;

arr = arr.filter(function(item) { return !(/^(test|System)/g.test(item.ServiceId || ’’));});

標簽: JavaScript
相關文章:
国产综合久久一区二区三区