使用lodash链过滤并返回父对象

时间:2019-03-27 13:27:21

标签: javascript typescript lodash

我想使用Lodash链函数来过滤数组中嵌套的数组项,然后返回完整的父对象。

这里有一些用例中的虚拟数据来说明我的问题:

const capital = [
  {
    "financeCategory": "Loans",
    "financeCategoryId": "22HM6fFFwx9eK2P42Onc",
    "financeElements": [
      {
        "financeCategoryId": "22HM6fFFwx9eK2P42Onc",
        "financeElementId": "JQiqqvGEugVQuI0fN1xQ",
        "financeElementTitle": "Convertible loan",
        "data": [
          {
            "month": 1,
            "value": 100,
            "year": "2020"
          },
          {
            "month": 1,
            "value": 100,
            "year": "2019"
          },
        ],

      }
    ]
  },
  {
    "financeCategory": "Investments",
    "financeCategoryId": "JtnUsk5M4oklIFk6cAlL",
    "financeElements": [] 
  },
  {
    "financeCategory": "Ownerships Contribution",
    "financeCategoryId": "PaDhGBm5uF0PhKJ1l6WX",
    "financeElements": []
  }
];

我要过滤FinanceElements中的“数据”数组,然后返回全部费用对象,并将过滤器应用于“数据”。

假设我要操作费用对象,仅获取具有2020年的FinanceElements上的数据。我已经尝试过:

const expenseFiltered: any = _.chain(expenses)
.flatMap('financeElements')
.flatMap('data')
.filter({year: '2020' as any}).value();

但这只是给了我过滤后的“数据”对象。 输出:

[{
            "month": 1,
            "value": 100,
            "year": "2020"
}]

现在,我知道有很多方法可以使用它来生成带有过滤数据的完整对象,但是我真的很想用一个简单的_.chain命令来做到这一点

所需的输出

[
  {
    "financeCategory": "Loans",
    "financeCategoryId": "22HM6fFFwx9eK2P42Onc",
    "financeElements": [
      {
        "financeCategoryId": "22HM6fFFwx9eK2P42Onc",
        "financeElementId": "JQiqqvGEugVQuI0fN1xQ",
        "financeElementTitle": "Convertible loan",
        "data": [
          {
            "month": 1,
            "value": 100,
            "year": "2020"
          }
        ],

      }
    ]
  },
  {
    "financeCategory": "Investments",
    "financeCategoryId": "JtnUsk5M4oklIFk6cAlL",
    "financeElements": [] 
  },
  {
    "financeCategory": "Ownerships Contribution",
    "financeCategoryId": "PaDhGBm5uF0PhKJ1l6WX",
    "financeElements": []
  }
]

使用lodash链有可能吗?

1 个答案:

答案 0 :(得分:0)

一条链用于分几个步骤转换结构。就您而言,您不想更改结构。您可以使用嵌套的Array.map()(或lodash的_.map())调用来迭代和重建结构,并在内部_.filter()进行数据:

const capital = [{"financeCategory":"Loans","financeCategoryId":"22HM6fFFwx9eK2P42Onc","financeElements":[{"financeCategoryId":"22HM6fFFwx9eK2P42Onc","financeElementId":"JQiqqvGEugVQuI0fN1xQ","financeElementTitle":"Convertible loan","data":[{"month":1,"value":100,"year":"2020"},{"month":1,"value":100,"year":"2019"}]}]},{"financeCategory":"Investments","financeCategoryId":"JtnUsk5M4oklIFk6cAlL","financeElements":[]},{"financeCategory":"Ownerships Contribution","financeCategoryId":"PaDhGBm5uF0PhKJ1l6WX","financeElements":[]}];

const expenseFiltered = capital.map(ex => ({
  ...ex,
  financeElements: ex.financeElements.map(fe => ({
    ...fe,
    data: _.filter(fe.data, { year: '2020' })
  }))
}));

console.log(expenseFiltered);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>