您好,我是Java脚本的新手,我正在使用knex ORM与数据库进行交互,并且由于结果行而导致无法使用平面json,因此我需要以预期的实际格式获取结果结果。我已经尝试了一些代码,但是没有一个代码给我预期的结果。我已经提到了我尝试过的原型代码以及代码的结果,但这些代码的格式不是我想要的,我该如何获取我在EXPECTED结果部分中提到的实际结果,如何使用lodash或ES6达到相同的效果高阶函数。
var data= [
{order_id: 1,kot_id: 10,price: 20,quantity: 2},
{order_id: 1,kot_id: 10,price: 100,quantity: 1},
{order_id: 1,kot_id: 10,price: 150,quantity: 1},
{order_id: 1,kot_id: 11,price: 55,quantity: 1},
{order_id: 1,kot_id: 11,price: 250,quantity: 3},
]
let objects = {}
let arr = []
result = data.forEach((item,index) => {
if(!objects.order_id) {
objects = {order_id: item.order_id,list: []}
arr.push(objects)
}
if(!objects.kot_id) {
objects.list.push({kot_id: item.kot_id,kot_list: []})
}
objects.list[index].kot_list.push({price: item.price,quantity: item.quantity})
})
console.log(JSON.stringify(arr,null,2))
RESULT OF THE ABOVE CODE
[
{
"order_id": 1,
"list": [
{
"kot_id": 10,
"kot_list": [
{
"price": 20,
"quantity": 2
}
]
},
{
"kot_id": 10,
"kot_list": [
{
"price": 100,
"quantity": 1
}
]
},
{
"kot_id": 10,
"kot_list": [
{
"price": 150,
"quantity": 1
}
]
},
{
"kot_id": 11,
"kot_list": [
{
"price": 55,
"quantity": 1
}
]
},
{
"kot_id": 11,
"kot_list": [
{
"price": 250,
"quantity": 3
},
]
}
]
}
]
EXPECTED RESULT
result = [
{
order_id: 1,
list: [
{
kot_id: 10,
kot_list : [
{ price: 200,quantity: 2 },
{price: 100,quantity: 1},
{price: 150,quantity: 1}
]
},
{
kot_id: 11,
kot_list: [
{price: 55,quantity: 1},
{price: 250,quantity: 3}
]
}
]
}
]
答案 0 :(得分:1)
lodash之类的实用工具,例如groupBy和mapValues。
Lodash示例:
const data = [
{order_id: 1, kot_id: 10, price: 20, quantity: 2},
{order_id: 1, kot_id: 10, price: 100, quantity: 1},
{order_id: 1, kot_id: 10, price: 150, quantity: 1},
{order_id: 1, kot_id: 11, price: 55, quantity: 1},
{order_id: 1, kot_id: 11, price: 250, quantity: 3},
{order_id: 2, kot_id: 10, price: 150, quantity: 1},
{order_id: 2, kot_id: 11, price: 55, quantity: 1},
{order_id: 2, kot_id: 11, price: 250, quantity: 3},
]
const result = _(data)
.groupBy('order_id')
.mapValues((values, key) => {
return _(values)
.groupBy('kot_id')
.map((list, id) => ({
kot_id: id,
kot_list: _.map(list, i => _.pick(i, ['price', 'quantity']))
}))
.value();
})
.map((list, order_id) => ({order_id, list}))
.value();
console.log(JSON.stringify(result, null, 4));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
或者您可以简单地使用ES6实用程序来达到相同的结果:
普通ES6示例:
const data = [
{order_id: 1, kot_id: 10, price: 20, quantity: 2},
{order_id: 1, kot_id: 10, price: 100, quantity: 1},
{order_id: 1, kot_id: 10, price: 150, quantity: 1},
{order_id: 1, kot_id: 11, price: 55, quantity: 1},
{order_id: 1, kot_id: 11, price: 250, quantity: 3},
{order_id: 2, kot_id: 10, price: 150, quantity: 1},
{order_id: 2, kot_id: 11, price: 55, quantity: 1},
{order_id: 2, kot_id: 11, price: 250, quantity: 3},
]
const orderIds = [...new Set(data.map(i => i.order_id))];
const kotIds = [... new Set(data.map(i => i.kot_id))];
const result = orderIds.map(order_id => {
return {
order_id,
list: kotIds
.map(kot_id => {
return {
kot_id,
kot_list: data
.filter(i => i.order_id === order_id && i.kot_id === kot_id)
.map(i => ({price: i.price, quantity: i.quantity}))
}
})
};
});
console.log(JSON.stringify(result, null, 4));