使用 lodash 对嵌套组进行分组
Grouping nested groups using lodash
我有一个对象有点像这样:
{
"FORD": [
{
"ManufacturerName": "FORD",
"Model": "Focus",
"Price": "12345"
},
{
"ManufacturerName": "FORD",
"Model": "KA",
"Price": "11111"
},
{
"ManufacturerName": "FORD",
"Model": "Focus",
"Price": "22222"
}
],
"HONDA": [
{
"ManufacturerName": "HONDA",
"Model": "JAZZ",
"Price": "98765"
},
{
"ManufacturerName": "HONDA",
"Model": "JAZZ",
"Price": "33333"
}
]
}
我使用这样的 lodash 代码已经走到这一步了:
let myTest = _.chain(obj)
.sortBy('ManufacturerName')
.groupBy('ManufacturerName')
我正在尝试做的也是按模型对其进行分组,因此结果将如下所示:
{
"FORD": [
{
"Focus": [
{
"ManufacturerName": "FORD",
"Model": "Focus",
"Price": "12345"
},
{
"ManufacturerName": "FORD",
"Model": "Focus",
"Price": "12345" }
],
"KA": [
{
"ManufacturerName": "FORD",
"Model": "KA",
"Price": "11111"
}
]
}
],
"HONDA": [
{
"JAZZ": [
{
"ManufacturerName": "HONDA",
"Model": "JAZZ",
"Price": "33333" },
{
"ManufacturerName": "HONDA",
"Model": "JAZZ",
"Price": "98765" }
]
}
]
}
谢谢。
用 _.mapValues()
映射对象并按 Model
:
对每个数组 (v
) 进行分组
const obj = {"FORD":[{"ManufacturerName":"FORD","Model":"Focus","Price":"12345"},{"ManufacturerName":"FORD","Model":"KA","Price":"11111"},{"ManufacturerName":"FORD","Model":"Focus","Price":"22222"}],"HONDA":[{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"98765"},{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"33333"}]}
const result = _.mapValues(obj, v => _.groupBy(v, 'Model'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
或使用链条:
const obj = {"FORD":[{"ManufacturerName":"FORD","Model":"Focus","Price":"12345"},{"ManufacturerName":"FORD","Model":"KA","Price":"11111"},{"ManufacturerName":"FORD","Model":"Focus","Price":"22222"}],"HONDA":[{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"98765"},{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"33333"}]}
const result = _(obj)
.mapValues(v => _.groupBy(v, 'Model'))
.value()
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
我有一个对象有点像这样:
{
"FORD": [
{
"ManufacturerName": "FORD",
"Model": "Focus",
"Price": "12345"
},
{
"ManufacturerName": "FORD",
"Model": "KA",
"Price": "11111"
},
{
"ManufacturerName": "FORD",
"Model": "Focus",
"Price": "22222"
}
],
"HONDA": [
{
"ManufacturerName": "HONDA",
"Model": "JAZZ",
"Price": "98765"
},
{
"ManufacturerName": "HONDA",
"Model": "JAZZ",
"Price": "33333"
}
]
}
我使用这样的 lodash 代码已经走到这一步了:
let myTest = _.chain(obj)
.sortBy('ManufacturerName')
.groupBy('ManufacturerName')
我正在尝试做的也是按模型对其进行分组,因此结果将如下所示:
{
"FORD": [
{
"Focus": [
{
"ManufacturerName": "FORD",
"Model": "Focus",
"Price": "12345"
},
{
"ManufacturerName": "FORD",
"Model": "Focus",
"Price": "12345" }
],
"KA": [
{
"ManufacturerName": "FORD",
"Model": "KA",
"Price": "11111"
}
]
}
],
"HONDA": [
{
"JAZZ": [
{
"ManufacturerName": "HONDA",
"Model": "JAZZ",
"Price": "33333" },
{
"ManufacturerName": "HONDA",
"Model": "JAZZ",
"Price": "98765" }
]
}
]
}
谢谢。
用 _.mapValues()
映射对象并按 Model
:
v
) 进行分组
const obj = {"FORD":[{"ManufacturerName":"FORD","Model":"Focus","Price":"12345"},{"ManufacturerName":"FORD","Model":"KA","Price":"11111"},{"ManufacturerName":"FORD","Model":"Focus","Price":"22222"}],"HONDA":[{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"98765"},{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"33333"}]}
const result = _.mapValues(obj, v => _.groupBy(v, 'Model'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
或使用链条:
const obj = {"FORD":[{"ManufacturerName":"FORD","Model":"Focus","Price":"12345"},{"ManufacturerName":"FORD","Model":"KA","Price":"11111"},{"ManufacturerName":"FORD","Model":"Focus","Price":"22222"}],"HONDA":[{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"98765"},{"ManufacturerName":"HONDA","Model":"JAZZ","Price":"33333"}]}
const result = _(obj)
.mapValues(v => _.groupBy(v, 'Model'))
.value()
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>