ES6过滤数组中数组的元素

时间:2017-10-30 22:17:47

标签: javascript node.js ecmascript-6

我目前有2个阵列。每个数组都有另一个名为" url"。

的数组
{
  "entities": [
    {
      "id": 0,
      "companyName": "4-County Electric Power Assn",
      "type": "E",
      "state": "MS",
      "code": "106641MS",
      "url": [
        {
          "title": "4 County Electric",
          "link": "http://www.4county.org/",
          "href": "http://www.4county.org/"
        }
      ]
    },
    {
      "id": 1,
      "companyName": "ACTON WATER DISTRICT OFFICE",
      "type": "W",
      "state": "MA",
      "code": "W1771MA",
      "url": [
        {
          "title": "Home — Acton Water District",
          "link": "http://www.actonwater.com/",
          "href": "http://www.actonwater.com/"
        },
        {
          "title": "Contact Us — Acton Water District",
          "link": "http://www.actonwater.com/customer-service/contact-us",
          "href": "http://www.actonwater.com/customer-service/contact-us"
        }
      ]
    }
  ]
}

我尝试过滤每个url array并删除任何与过滤条件不匹配的项目。

我已经创建了一个过滤器,可以成功过滤掉不包含单词的网址"联系"

  const regex = new RegExp('/contact\\b', 'g');
  const companyColumn = db.get(`entities`).value()

  const filteredData = companyColumn.map((a ,i) => {
    return a.url.filter(({href}) => href.match(regex))
  })

并得到回复:

[ [],
  [ { title: 'Contact Us — Acton Water District',
      link: 'http://www.actonwater.com/customer-service/contact-us',
      description: 'Office hours are Monday–Friday, 7:30 AM until 4:00 PM (excluding holidays) We \nare located at 693 Massachusetts Avenue, Acton, Ma 01720. Our mailing ...',
      href: 'http://www.actonwater.com/customer-service/contact-us' } ] ]

热潮。所以它有效。

但我的问题是如何设置"实体"中的第一项?数组为空URL array,而第二个"实体" item中只有1个项目URL array

我感觉如此亲密......

0 个答案:

没有答案