搜索
javascript - js如何将匹配到的数组元素删掉?
扔个三星炸死你
扔个三星炸死你 2017-06-19 09:07:51
[Node.js讨论组]
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开头的数组元素删掉
    用删掉的方法总是没法讲匹配到的全删,哪位高手能帮个忙呢?谢谢!
扔个三星炸死你
扔个三星炸死你

全部回复(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)); 
}
仅有的幸福

Array.filter方法,将过滤后的数组赋值回arr

arr = arr.filter(function(item) {
    return !(/^(test|System)/g.test(item.ServiceId || ''));
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号