编程就是不同格式对象的转换
上周有同事提出对象转换的问题,觉得难以解决,让我帮忙看下,需求很清晰
var a = [
{
"appId": 1,
"appName": "分组1",
"ip": "172.0.0.1"
},
{
"appId": 1,
"appName": "分组1",
"ip": "172.0.0.2"
},
{
"appId": 2,
"appName": "分组2",
"ip": "172.0.0.3"
},
{
"appId": 2,
"appName": "分组2",
"ip": "172.0.0.4"
},
{
"appId": 3,
"appName": "分组3",
"ip": "172.0.0.5"
},
{
"appId": 3,
"appName": "分组3",
"ip": "172.0.0.6"
}
]
var b = [
{
id: 1,
label: '分组1',
appId: 1,
appName: '分组1',
children: [
{
id: 2,
label: '172.0.0.1',
appId: 1,
appName: '分组1',
ip: '172.0.0.1',
}, {
id: 3,
label: '172.0.0.2',
appId: 1,
appName: '分组1',
ip: '172.0.0.2',
}
]
},
{
id: 4,
label: '分组2',
appId: 2,
appName: '分组2',
children: [
{
id: 5,
label: '172.0.0.3',
appId: 2,
appName: '分组2',
ip: '172.0.0.3',
}, {
id: 6,
label: '172.0.0.4',
appId: 2,
appName: '分组2',
ip: '172.0.0.4',
}
]
},
{
id: 7,
label: '分组3',
appId: 3,
appName: '分组3',
children: [
{
id: 8,
label: '172.0.0.5',
appId: 3,
appName: '分组3',
ip: '172.0.0.5',
}, {
id: 9,
label: '172.0.0.6',
appId: 3,
appName: '分组3',
ip: '172.0.0.6',
}
]
},
];
把对象a转换为对象b
我一看,显然这个转换分为两部:1)分组 2)对每个分组进行转换
于是按照传统代码思维夹杂着一些不成熟的函数式编程思想,我写了如下代码:
var a = [
{
"appId": 1,
"appName": "分组1",
"ip": "172.0.0.1"
},
{
"appId": 1,
"appName": "分组1",
"ip": "172.0.0.2"
},
{
"appId": 2,
"appName": "分组2",
"ip": "172.0.0.3"
},
{
"appId": 2,
"appName": "分组2",
"ip": "172.0.0.4"
},
{
"appId": 3,
"appName": "分组3",
"ip": "172.0.0.5"
},
{
"appId": 3,
"appName": "分组3",
"ip": "172.0.0.6"
}
]
var map = new Map();
a.forEach((item) => {
if(map.get(item.appId) == undefined) {
map.set(item.appId, []);
}
map.get(item.appId).push(item)
});
var result = []
map.forEach((value, key) => {
var obj = {};
obj.id = key;
obj.label = value[0]["appName"];
obj.appId = key;
obj.appName = value[0]["appName"];
obj.children = [];
value.forEach((item) => {
obj.children.push({
id: key,
label: item["ip"],
appId: item["appId"],
appName: item["appName"],
ip: item["ip"]
})
})
result.push(obj)
})
console.info(result)
回去看了函数编程,纯函数,科里化等概念后,感觉之前的代码写得不好,于是做了如下改进:
var a = [
{
"appId": 1,
"appName": "分组1",
"ip": "172.0.0.1"
},
{
"appId": 1,
"appName": "分组1",
"ip": "172.0.0.2"
},
{
"appId": 2,
"appName": "分组2",
"ip": "172.0.0.3"
},
{
"appId": 2,
"appName": "分组2",
"ip": "172.0.0.4"
},
{
"appId": 3,
"appName": "分组3",
"ip": "172.0.0.5"
},
{
"appId": 3,
"appName": "分组3",
"ip": "172.0.0.6"
}
]
var m1 = (origin) => origin.reduce((dic, item) => {
dic[item.appId] = (!dic[item.appId] ? [item] : [...dic[item.appId], item]);
return dic;
}, {});
var m2 = (origin) => Object.keys(origin).reduce((acc, item) => [...acc, {children: origin[item].map(item => ({id: item.appId, label: item.ip, ...item})), ...{id: origin[item][0].appId, label: origin[item][0].appName, appId: origin[item][0].appId, appName: origin[item][0].appName}}], [])
var combine = (...fn) => origin => fn.reduce((acc, fn) => fn(acc), origin)
var combineM = combine(m1, m2)
var b = combineM(a)
可读性差了很多,但是代码更清晰了,也更符合函数式编程思想了
一个比较折中的方案:
var a = [
{
"appId": 1,
"appName": "分组1",
"ip": "172.0.0.1"
},
{
"appId": 1,
"appName": "分组1",
"ip": "172.0.0.2"
},
{
"appId": 2,
"appName": "分组2",
"ip": "172.0.0.3"
},
{
"appId": 2,
"appName": "分组2",
"ip": "172.0.0.4"
},
{
"appId": 3,
"appName": "分组3",
"ip": "172.0.0.5"
},
{
"appId": 3,
"appName": "分组3",
"ip": "172.0.0.6"
}
]
var m1 = arr => arr.reduce((acc, item) => {
if(!acc[item.appId]) {
acc[item.appId] = [];
}
acc[item.appId] = [...acc[item.appId], item];
return acc;
}, {})
var m2 = obj => {
let arr = Object.keys(obj);
return arr.reduce((acc, item) => {
let itemm = obj[item];
let item0 = itemm[0];
let objj = {id: item0.appId, label: item0.ip, appId: item0.appId, appName: item0.appName};
let children = { children: itemm.map((item) => ({id: item.appId, label: item.appName, ...item}))}
return [...acc, {...objj, ...children}]
}, [])
}
var b = m2(m1(a))