javascript - How to delete matched array elements in js?
扔个三星炸死你
扔个三星炸死你 2017-06-19 09:07:51
0
3
742
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开头的数组元素删掉 用删掉的方法总是没法讲匹配到的全删,哪位高手能帮个忙呢?谢谢!
扔个三星炸死你
扔个三星炸死你

reply all (3)
某草草

arr = arr.filter(item => !(/^test|^System/i.test(item.ServiceID)))

    刘奇
    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)); }
      仅有的幸福

      Use theArray.filtermethod to assign the filtered array back toarr;

      arr = arr.filter(function(item) { return !(/^(test|System)/g.test(item.ServiceId || '')); });
        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!