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

时间:2018-11-02 12:03:09

标签: javascript regex lodash

我有一个这样的数据数组:

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 data = [
  {Date: '2018010101', color: 'blue'},
  {Date: '2018010102', color: 'blue'},
  {Date: '2018010103', color: 'red'},
  {Date: '2018010104', color: 'green'},
]

所以我想过滤数组并只获取带有“日期”字段的对象,其中包含“ 20180101 *”。

我该怎么办? 我想我可以使用lodash过滤数组,但是我不知道如何设置正则表达式。这是我写的:

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

3 个答案:

答案 0 :(得分:2)

您可以使用正则表达式执行类似的操作。在这里,我根据具有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; }

答案 1 :(得分:0)

使用Lodash可以做到这一点。您可以使用filter,过滤器和conformstakeWhile等。

以下是一些示例:

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>

使用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)

我还添加了startsWith,以防万一,因为从您的示例数据中似乎并不需要regEx,但是您可能没有提供所有数据。

答案 2 :(得分:-1)

使用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)