简体   繁体   English

如何从数组中删除所有不包含包含特定字符串的字段的元素?

[英]How to remove all elements from array that do not have field containing specific string?

This is some of the result obtained from an ElasticSearch query .. 这是从ElasticSearch查询中获得的一些结果。

"buckets": [{
    "key": "another_service_name:0.0.1",
    "doc_count": 105,
    "containers": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
            "key": "7eaf4933b0366f7212f30f9f3c315672ea18e026922d0651b29b844763be6b0a",
            "doc_count": 105,
            "someField": {
                "value": 1.2708647376015072
            }
        }]
    }
}, {
    "key": "my_service_name:0.0.1",
    "doc_count": 200,
    "containers": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
            "key": "4c27931147386d49e90f581a1d5cb22f53cda00b16107864cad9eff32f61d776",
            "doc_count": 100,
            "someField": {
                "value": 77.9621390914917
            }
        }, {
            "key": "392e7e6981cd4c24c526ffda03977b7984ebcae58194680c82940af4acec4748",
            "doc_count": 100,
            "someField": {
                "value": 76.63604362487793
            }
        }]
    }
}]

Is there a simple 1-liner to remove any elements from the outter buckets array that do not have the key containing my_service_name ? 是否有一个简单的1-liner可以从外部buckets数组中删除任何不包含包含my_service_namekeymy_service_name

I know I could loop over the array, use indexOf and splice .. but is there a shorter way? 我知道我可以遍历数组,使用indexOfsplice ..但有没有更短的方法?

I would like the result to look like this .. 我希望结果看起来像这样..

"buckets": [{
    "key": "my_service_name:0.0.1",
    "doc_count": 200,
    "containers": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
            "key": "4c27931147386d49e90f581a1d5cb22f53cda00b16107864cad9eff32f61d776",
            "doc_count": 100,
            "someField": {
                "value": 77.9621390914917
            }
        }, {
            "key": "392e7e6981cd4c24c526ffda03977b7984ebcae58194680c82940af4acec4748",
            "doc_count": 100,
            "someField": {
                "value": 76.63604362487793
            }
        }]
    }
}]

You could use the Array.prototype.filter method: 您可以使用Array.prototype.filter方法:

var queryResults = /* your result as above */;

var myServices = queryResults.filter(function(element) {
    return element.key.indexOf('my_service_name') >= 0;
})

In ES6 it's possible to shorten this to: 在ES6中,可以将其缩短为:

let queryResults = /* your result as above */;

let myServices = queryResults.filter(element => element.key.includes('my_service_name'))

A one-liner can be the Array.filter method, like this: 单行代码可以是Array.filter方法,如下所示:

buckets.filter( item => /my_service_name:/i.test(item.key) );

As lonesomeday mentions, the arrow function is not working in Internet Explorer (and filter might not be working to), to still use this almost one liner, you could rewrite it as: 正如lonesomeday所提到的,箭头功能在Internet Explorer中不起作用(并且过滤器可能不起作用),要仍然使用这几乎一根衬垫,可以将其重写为:

buckets.filter( function(item) { return /my_service_name:/i.test( item.key ); } );

To support filter in Internet Explorer, you can use the polyfill supplied on the MDN page 为了支持过滤器在Internet Explorer中,您可以用填充工具的MDN页面上提供

 var buckets = [{ "key": "another_service_name:0.0.1", "doc_count": 105, "containers": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [{ "key": "7eaf4933b0366f7212f30f9f3c315672ea18e026922d0651b29b844763be6b0a", "doc_count": 105, "someField": { "value": 1.2708647376015072 } }] } }, { "key": "my_service_name:0.0.1", "doc_count": 200, "containers": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [{ "key": "4c27931147386d49e90f581a1d5cb22f53cda00b16107864cad9eff32f61d776", "doc_count": 100, "someField": { "value": 77.9621390914917 } }, { "key": "392e7e6981cd4c24c526ffda03977b7984ebcae58194680c82940af4acec4748", "doc_count": 100, "someField": { "value": 76.63604362487793 } }] } }]; console.log( buckets.filter( item => /my_service_name:/i.test(item.key) ) ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM