使用 lodash 格式化数据
data formatting with lodash
假设我有以下 json 数组:
const input = [
{ "tx_type": "215", "dos": "2019-05-02", "payer": "Cigna", "count": 23 },
{ "tx_type": "215", "dos": "2019-05-02", "payer": "SENIORCARE Plus", "count": 75 },
{ "tx_type": "217", "dos": "2019-05-02", "payer": "Aetna", "count": 2 },
{ "tx_type": "215", "dos": "2019-05-03", "payer": "Aetna", "count": 85 },
{ "tx_type": "215", "dos": "2019-05-03", "payer": "TRICARE", "count": 1 },
{ "tx_type": "215", "dos": "2019-05-03", "payer": "Aetna", "count": 5 },
{ "tx_type": "215", "dos": "2019-05-03", "payer": "Cigna", "count": 11 }
]
它来自 postgres 9.2 db,但我正在尝试将数据放入一个 dataviz 中,它希望数据看起来像:
[
{
"tx_type": "x215",
"dos": [
{ "date": "2019-05-02", "SENIORCARE Plus": 75, "Cigna": 23 },
{ "date": "2019-05-03", "Aetna": 96, "TRICARE": 1, "Cigna": 11 }
],
},
{
"tx_type": "x215",
"dos": [
{ "date": "2019-05-02", "Aetna": 2 }
]
}
]
我已经尝试使用 lodash 通过 tx_type 使用 .groupBy("tx_type")
以及 _.chain(input).nest("tx_type").groupBy("dos").value()
对对象进行分组,以及通过 tx_type 进行过滤然后尝试组/巢...
真的,我想做的就是按 tx_type 过滤并按日期对付款人和计数进行分组。
如有任何意见,我们将不胜感激。尽管我想升级到更新版本的 postgres 并不是一个真正的选择..
分组 tx_type
并映射组。要创建 dos
属性,映射组中的项目,按 dos
对它们进行分组,映射结果以将每一行转换为 { data, [payer]:count }
的对象,然后合并对象:
const input = [{"tx_type":"215","dos":"2019-05-02","payer":"Cigna","count":23},{"tx_type":"215","dos":"2019-05-02","payer":"SENIORCARE Plus","count":75},{"tx_type":"217","dos":"2019-05-02","payer":"Aetna","count":2},{"tx_type":"215","dos":"2019-05-03","payer":"Aetna","count":85},{"tx_type":"215","dos":"2019-05-03","payer":"TRICARE","count":1},{"tx_type":"215","dos":"2019-05-03","payer":"Aetna","count":5},{"tx_type":"215","dos":"2019-05-03","payer":"Cigna","count":11}]
const result = _(input)
.groupBy('tx_type')
.map((dos, tx_type) => ({
tx_type,
dos: _(dos)
.groupBy('dos')
.map((g, date) => _.merge({},
..._.map(g, ({ payer, count }) => ({ date, [payer]: count }))
))
.value()
}))
.value()
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash-compat/3.10.2/lodash.js"></script>
假设我有以下 json 数组:
const input = [
{ "tx_type": "215", "dos": "2019-05-02", "payer": "Cigna", "count": 23 },
{ "tx_type": "215", "dos": "2019-05-02", "payer": "SENIORCARE Plus", "count": 75 },
{ "tx_type": "217", "dos": "2019-05-02", "payer": "Aetna", "count": 2 },
{ "tx_type": "215", "dos": "2019-05-03", "payer": "Aetna", "count": 85 },
{ "tx_type": "215", "dos": "2019-05-03", "payer": "TRICARE", "count": 1 },
{ "tx_type": "215", "dos": "2019-05-03", "payer": "Aetna", "count": 5 },
{ "tx_type": "215", "dos": "2019-05-03", "payer": "Cigna", "count": 11 }
]
它来自 postgres 9.2 db,但我正在尝试将数据放入一个 dataviz 中,它希望数据看起来像:
[
{
"tx_type": "x215",
"dos": [
{ "date": "2019-05-02", "SENIORCARE Plus": 75, "Cigna": 23 },
{ "date": "2019-05-03", "Aetna": 96, "TRICARE": 1, "Cigna": 11 }
],
},
{
"tx_type": "x215",
"dos": [
{ "date": "2019-05-02", "Aetna": 2 }
]
}
]
我已经尝试使用 lodash 通过 tx_type 使用 .groupBy("tx_type")
以及 _.chain(input).nest("tx_type").groupBy("dos").value()
对对象进行分组,以及通过 tx_type 进行过滤然后尝试组/巢...
真的,我想做的就是按 tx_type 过滤并按日期对付款人和计数进行分组。
如有任何意见,我们将不胜感激。尽管我想升级到更新版本的 postgres 并不是一个真正的选择..
分组 tx_type
并映射组。要创建 dos
属性,映射组中的项目,按 dos
对它们进行分组,映射结果以将每一行转换为 { data, [payer]:count }
的对象,然后合并对象:
const input = [{"tx_type":"215","dos":"2019-05-02","payer":"Cigna","count":23},{"tx_type":"215","dos":"2019-05-02","payer":"SENIORCARE Plus","count":75},{"tx_type":"217","dos":"2019-05-02","payer":"Aetna","count":2},{"tx_type":"215","dos":"2019-05-03","payer":"Aetna","count":85},{"tx_type":"215","dos":"2019-05-03","payer":"TRICARE","count":1},{"tx_type":"215","dos":"2019-05-03","payer":"Aetna","count":5},{"tx_type":"215","dos":"2019-05-03","payer":"Cigna","count":11}]
const result = _(input)
.groupBy('tx_type')
.map((dos, tx_type) => ({
tx_type,
dos: _(dos)
.groupBy('dos')
.map((g, date) => _.merge({},
..._.map(g, ({ payer, count }) => ({ date, [payer]: count }))
))
.value()
}))
.value()
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash-compat/3.10.2/lodash.js"></script>