简体   繁体   English

使用正则表达式过滤对象数组

[英]Filter array of objects using regex

I have an array of data like this: 我有这样的数据数组:

const data = [
  {Date: '2018010101', color: 'blue'},
  {Date: '2018010102', color: 'blue'},
  {Date: '2018010103', color: 'red'},
  {Date: '2018010104', color: 'green'},
  {Date: '2018010202', color: 'red'},
  {Date: '2018010301', color: 'yellow'},
  {Date: '2018010204', color: 'green'},
  {Date: '2018010305', color: 'blue'},
  {Date: '2018010206', color: 'green'},
]

now I want to get an array of this type: 现在我想得到一个这种类型的数组:

const data = [
  {Date: '2018010101', color: 'blue'},
  {Date: '2018010102', color: 'blue'},
  {Date: '2018010103', color: 'red'},
  {Date: '2018010104', color: 'green'},
]

so I want to filter the array and get only those objects with “Date” field that contains 20180101*”. 因此,我想过滤数组并仅获取带有“日期”字段且包含“ 20180101 *”的对象。

how can I do? 我能怎么做? I think I could use lodash to filter the array but I don't know how to set the regex. 我想我可以使用lodash过滤数组,但是我不知道如何设置正则表达式。 This is what I wrote: 这是我写的:

const filterBy = { 'Date': '20180101*' }
const filteredData = filter(data, filterBy)

You can do something like this with regex. 您可以使用regex做类似的事情。 Here I am filtering based on the dates which have 20180101 as a prefix with anything following as a suffix: 在这里,我基于具有20180101作为前缀的日期进行过滤,其后缀为:

 const data = [ {Date: '2018010101', color: 'blue'}, {Date: '2018010102', color: 'blue'}, {Date: '2018010103', color: 'red'}, {Date: '2018010104', color: 'green'}, {Date: '2018010202', color: 'red'}, {Date: '2018010301', color: 'yellow'}, {Date: '2018010204', color: 'green'}, {Date: '2018010305', color: 'blue'}, {Date: '2018010206', color: 'green'}], res = data.filter(({Date}) => Date.match(/20180101./g)); console.log(res); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

With Lodash there are more than one way to do this. 使用Lodash,有多种方法可以做到这一点。 You can use filter , filter & conforms , takeWhile etc. 您可以使用过滤器 ,过滤器和符合takeWhile等。

Here are some examples: 这里有些例子:

 const data = [ {Date: '2018010101', color: 'blue'}, {Date: '2018010102', color: 'blue'}, {Date: '2018010103', color: 'red'}, {Date: '2018010104', color: 'green'}, {Date: '2018010202', color: 'red'}, {Date: '2018010301', color: 'yellow'}, {Date: '2018010204', color: 'green'}, {Date: '2018010305', color: 'blue'}, {Date: '2018010206', color: 'green'}, ] const regEx = /20180101/g const ld1 = _.filter(data, ({Date}) => !!Date.match(regEx)) const ld2 = _.filter(data, _.conforms({ 'Date': d => d.match(regEx) })) const ld3 = _.takeWhile(data, ({Date}) => !!Date.match(regEx)) console.log('filter', ld1) console.log('filter & conforms', ld2) console.log('takeWhile', ld3) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script> 

With ES6 you could just: 使用ES6,您可以:

 const data = [ {Date: '2018010101', color: 'blue'}, {Date: '2018010102', color: 'blue'}, {Date: '2018010103', color: 'red'}, {Date: '2018010104', color: 'green'}, {Date: '2018010202', color: 'red'}, {Date: '2018010301', color: 'yellow'}, {Date: '2018010204', color: 'green'}, {Date: '2018010305', color: 'blue'}, {Date: '2018010206', color: 'green'}, ] const regEx = /20180101/g const es61 = data.filter(({Date}) => !!Date.match(regEx)) const es62 = data.filter(({Date}) => Date.startsWith('20180101')) // just in case console.log('filter', es61) console.log('startsWith', es62) 

I also added the startsWith just in case since from your sample data it seems regEx is not exactly needed but you might have not provided all the data. 我还添加了startsWith ,以防万一,因为从您的示例数据来看,似乎并不需要regEx,但您可能未提供所有数据。

Use Array.filter() with indexOf() to check that filter key in the Date value: 使用Array.filter()indexOf()来检查Date值中的过滤键:

 const data = [ {Date: '2018010101', color: 'blue'}, {Date: '2018010102', color: 'blue'}, {Date: '2018010103', color: 'red'}, {Date: '2018010104', color: 'green'}, {Date: '2018010202', color: 'red'}, {Date: '2018010301', color: 'yellow'}, {Date: '2018010204', color: 'green'}, {Date: '2018010305', color: 'blue'}, {Date: '2018010206', color: 'green'}, ]; let filterKey = '20180101'; const res = data.filter(item => item.Date.indexOf(filterKey) === 0); console.log(res) 

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

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