使用Lodash按键名过滤对象列表

时间:2015-10-14 13:16:33

标签: lodash

我有一个对象列表,如下所示:

[

      {
        "account_id": "1002",
        "fields": [
          {
            "text_value": "2/3/87",
            "name": "birth_date"
          },
          {
            "text_value": "Dennis",
            "name": "first_name"
          },
          {
            "text_value": "Monsewicz",
            "name": "last_name"
          },
          {
            "text_value": "White",
            "name": "socks"
          }
        ],
        "created_at": "2015-10-08T14:31:07.306000",
        "archived_at": null,
        "email": "foo.bar+09202902902942@gmail.com",
        "contact_id": "3bd41146-7157-4bd1-b8dc-b85c5bd765a4",
        "status": "active"
      },
      {
        "account_id": "1002",
        "fields": [
          {
            "text_value": "2/3/87",
            "name": "birth_date"
          },
          {
            "text_value": "Dennis",
            "name": "first_name"
          },
          {
            "text_value": "Monsewicz",
            "name": "last_name"
          },
          {
            "text_value": "White",
            "name": "socks"
          }
        ],
        "created_at": "2015-10-08T14:34:12.874000",
        "archived_at": null,
        "email": "ff.lol+lololpopo@gmail.com",
        "contact_id": "abf0dc16-2e54-45e3-9bed-c0784da645ba",
        "status": "active"
      },
      {
        "account_id": "1002",
        "fields": [
          {
            "text_value": "gibberish",
            "name": "birth_date"
          }
        ],
        "created_at": "2015-10-08T19:56:50.208000",
        "archived_at": null,
        "email": "bar.baz+dksjhfs@gmail.com",
        "contact_id": "ff3ee87a-b7a7-4b1f-98dd-307bf3c10d06",
        "status": "active"
      },
      {
        "account_id": "1002",
        "fields": [
          {
            "text_value": "1",
            "name": "city"
          }
        ],
        "created_at": "2015-10-08T20:02:46.546000",
        "archived_at": null,
        "email": "bar.bax+2dwksjhfs@gmail.com",
        "contact_id": "98b6be26-6fa2-4d8e-8776-2ab300e9a4b0",
        "status": "active"
      },
      {
        "account_id": "1002",
        "fields": [
          {
            "datetime_value": "1987-02-03T00:00:00",
            "name": "birth_date"
          }
        ],
        "created_at": "2015-10-08T19:59:18.189000",
        "archived_at": null,
        "email": "foo.pop+2dksjhfs@gmail.com",
        "contact_id": "59f1bcbf-e62a-4705-bb80-dd96a6fa5976",
        "status": "active"
      }
    ]

以上数据由我正在处理的API中的端点返回。我将允许用户添加filter_columns的查询参数,如果他们愿意,他们可以返回某些信息。

因此,网址看起来像http://my-api.com/1002/contacts?filter_columns=["first_name", "last_name"]。我基本上试图过滤出fields数组,其中name等于filter_columns数组中的列。我正在尝试使用_.matchesPropertyhttp://devdocs.io/lodash/index#matchesProperty)的lodash方法,但我不确定如何做我想做的事

1 个答案:

答案 0 :(得分:0)

你需要总共进行3次迭代才能完成你想要的任务。

  1. 了解您从API端点获取的data的所有元素
  2. fields
  3. 的所有元素
  4. filter_columns
  5. 的所有元素

    我会推荐这个解决方案:

    _.each(data, function(i) {
      _.remove(i.fields, function(f) {
        return _.includes(filter_columns, f.name)
      })
    })
    

    或使用ECMAScript 6:

    _.each(data, i => {
      _.remove(i.fields, f =>
        _.includes(filter_columns, f.name)
      )
    })
    
    如果matchesProperty是单个字符串而不是数组,那么

    filter_columns或类似样式的回调将非常有用。如果你有一个名为filter_value的字符串,你可以做(​​总共2次迭代):

    _.each(data, function(i) {
      _.remove(i.fields, 'name', filter_value)
    })
    

    在这种情况下,使用matchesProperty样式回调,filter_valuename属性匹配