如何递归地将具有属性的数组对象分类为子数组对象?
How to category with properties array object into sub-array object recursively?
我使用来自数据库执行的 dataObj 对这个映射函数进行参数化 return。
function(dataObj){
obj.gateways = dataObj.map((item) => ({
gw_name: item.gw_name.toUpperCase(),
pm_id: item.pm_uid,
pm_name: item.description.toUpperCase(),
pm_is_card: item.is_card_pmt ? true : false,
device_id: item.pd_uid,
device_name: item.pd_name,
}));
return obj;
}
我需要将上面的函数 return arrayObject 转换成这样的东西,
gateways: [
{
gw_name: "GATEWAY_1",
gw_methods: [
{
pm_id: "234567",
pm_name: "DEBIT CARD",
pm_is_card: true
},
{
pm_id: "123456",
pm_name: "CREDIT CARD",
pm_is_card: true
},
],
gw_devices:[
{
device_id: 1234,
device_name: "Device_1"
},
{
device_id: 56789,
device_name: "Device_2"
}
]
},
{
gw_name: "GATEWAY_2",
gw_methods: [
{
pm_id: "098763",
pm_name: "WEB",
pm_is_card: false
},
{
pm_id: "123456",
pm_name: "CREDIT CARD",
pm_is_card: true
},
],
gw_devices:[
{
device_id: 56789,
device_name: "Device_2"
},
{
device_id: 8787,
device_name: "Device_3"
},
]
},
]
我尝试了很多方法,但没有运气,任何建议将不胜感激。
谢谢!
鉴于数据如下:
const data = [
{
gw_name: "GATEWAY_1",
pm_id: "234567",
pm_name: "DEBIT CARD",
pm_is_card: true,
},
{
gw_name: "GATEWAY_1",
pm_id: "123456",
pm_name: "CREDIT CARD",
pm_is_card: true,
},
{
gw_name: "GATEWAY_2",
pm_id: "098763",
pm_name: "WEB",
pm_is_card: false,
},
{
gw_name: "GATEWAY_2",
pm_id: "123456",
pm_name: "CREDIT CARD",
pm_is_card: true,
},
];
以下 reduce 会对您有所帮助:
const gateways = data.reduce((memo, item) => {
let target = memo.find(({ gw_name }) => gw_name === item.gw_name);
if (!target) {
target = {
gw_name: item.gw_name,
gw_methods: [item],
};
return [...memo, target];
}
target.gw_methods = [...target.gw_methods, item];
return memo;
}, []);
我使用来自数据库执行的 dataObj 对这个映射函数进行参数化 return。
function(dataObj){
obj.gateways = dataObj.map((item) => ({
gw_name: item.gw_name.toUpperCase(),
pm_id: item.pm_uid,
pm_name: item.description.toUpperCase(),
pm_is_card: item.is_card_pmt ? true : false,
device_id: item.pd_uid,
device_name: item.pd_name,
}));
return obj;
}
我需要将上面的函数 return arrayObject 转换成这样的东西,
gateways: [
{
gw_name: "GATEWAY_1",
gw_methods: [
{
pm_id: "234567",
pm_name: "DEBIT CARD",
pm_is_card: true
},
{
pm_id: "123456",
pm_name: "CREDIT CARD",
pm_is_card: true
},
],
gw_devices:[
{
device_id: 1234,
device_name: "Device_1"
},
{
device_id: 56789,
device_name: "Device_2"
}
]
},
{
gw_name: "GATEWAY_2",
gw_methods: [
{
pm_id: "098763",
pm_name: "WEB",
pm_is_card: false
},
{
pm_id: "123456",
pm_name: "CREDIT CARD",
pm_is_card: true
},
],
gw_devices:[
{
device_id: 56789,
device_name: "Device_2"
},
{
device_id: 8787,
device_name: "Device_3"
},
]
},
]
我尝试了很多方法,但没有运气,任何建议将不胜感激。 谢谢!
鉴于数据如下:
const data = [
{
gw_name: "GATEWAY_1",
pm_id: "234567",
pm_name: "DEBIT CARD",
pm_is_card: true,
},
{
gw_name: "GATEWAY_1",
pm_id: "123456",
pm_name: "CREDIT CARD",
pm_is_card: true,
},
{
gw_name: "GATEWAY_2",
pm_id: "098763",
pm_name: "WEB",
pm_is_card: false,
},
{
gw_name: "GATEWAY_2",
pm_id: "123456",
pm_name: "CREDIT CARD",
pm_is_card: true,
},
];
以下 reduce 会对您有所帮助:
const gateways = data.reduce((memo, item) => {
let target = memo.find(({ gw_name }) => gw_name === item.gw_name);
if (!target) {
target = {
gw_name: item.gw_name,
gw_methods: [item],
};
return [...memo, target];
}
target.gw_methods = [...target.gw_methods, item];
return memo;
}, []);