Lodash 链接?
Lodash chaining?
我有一个 JSON 对象,看起来像这样:
{
"ReceiptNumber": 2129,
"ReceiptResult": 1,
"ReceiptAmount": "6756",
"ReceiptData": [
{
"LineNum": "JonSnow",
"Data": "Standard data for Jon Snow"
},
{
"LineNum": "HarryPotter",
"Data": "Standard data for Harry Potter"
},
{
"LineNum": "MickyMouse",
"Data": "Standard data for Micky Mouse"
}
],
"ReceiptReference": "22e06e66-e711-bd14-7874a-002219649f24"
}
我正在尝试将其转换为:
{
"receiptNumber": 2129,
"receiptResult": 1,
"receiptAmount": "6756",
"receiptData": {
"jonSnow": "Standard data for Jon Snow",
"harryPotter": "Standard data for Harry Potter",
"mickyMouse": "Standard data for Micky Mouse"
},
"receiptReference": "22e06e66-e711-bd14-7874a-002219649f24"
}
我起诉的方法:
getFromReceipt(result): void {
result.ReceiptData = _.assign({}, ...result.ReceiptData.map(el => {
return { [_.camelCase(el.LineNum)]: el.Data }
}));
this.camelCaseAllKeys(result);
}
camelCaseAllKeys(result) {
return _.mapKeys(result, (v, k) => _.camelCase(k.toString()));
}
有没有什么方法可以简化这些方法并最终通过链接在一个函数中完成所有事情?
评论太长了。
您在一个函数中混淆了多个问题:
- 展平 ReceiptData
- 驼峰式按键
尚不清楚这些东西是否应该在同一个函数中:虽然它是更短的代码,但这是一种虚假的经济。一旦你通过 minifier + gzip 运行 它,差异可以忽略不计,虽然较短的代码通常更容易阅读,但混淆不同的关注点会使它变得更难。除非你的数据集真的很大(在这种情况下你为什么要在客户端处理它?)对运行时间性能的影响可以忽略不计。
所以我会推荐更多这样的东西:
let data = howeverYouGotTheData;
let flattenReceiptData = o => {
o.ReceiptData = o.ReceiptData.reduce((acc, record) => {
acc[_.camelCase(record.lineNum)] = record.Data;
return acc;
}, {});
return o;
};
let camelCaseKeys = o => {
return Object.entries(o).reduce((acc, [k, v]) => {
acc[_.camelCase(k)] = v;
return acc;
}, {});
};
// _.flow is lodash's implementation of left composition
let flattenAndCase = _.flow(flattenReceiptData, camelCaseKeys);
let result = flattenAndCase(data);
现在关注点被适当地分离了,一切都被清楚地命名并且易于阅读,等等。如果你真的想打高尔夫球,你可以使用像这样的一些标准技巧:
let cc = _.camelCase;
let flattenRD = o => (o.ReceiptData = o.ReceiptData.reduce((a, x) => (a[cc(x.lineNum)] = x.Data, a), {}));
等等。我使用逗号运算符将赋值和 return 压缩为一个表达式,从而避免了对 Object.assign
.
的需要
我有一个 JSON 对象,看起来像这样:
{
"ReceiptNumber": 2129,
"ReceiptResult": 1,
"ReceiptAmount": "6756",
"ReceiptData": [
{
"LineNum": "JonSnow",
"Data": "Standard data for Jon Snow"
},
{
"LineNum": "HarryPotter",
"Data": "Standard data for Harry Potter"
},
{
"LineNum": "MickyMouse",
"Data": "Standard data for Micky Mouse"
}
],
"ReceiptReference": "22e06e66-e711-bd14-7874a-002219649f24"
}
我正在尝试将其转换为:
{
"receiptNumber": 2129,
"receiptResult": 1,
"receiptAmount": "6756",
"receiptData": {
"jonSnow": "Standard data for Jon Snow",
"harryPotter": "Standard data for Harry Potter",
"mickyMouse": "Standard data for Micky Mouse"
},
"receiptReference": "22e06e66-e711-bd14-7874a-002219649f24"
}
我起诉的方法:
getFromReceipt(result): void {
result.ReceiptData = _.assign({}, ...result.ReceiptData.map(el => {
return { [_.camelCase(el.LineNum)]: el.Data }
}));
this.camelCaseAllKeys(result);
}
camelCaseAllKeys(result) {
return _.mapKeys(result, (v, k) => _.camelCase(k.toString()));
}
有没有什么方法可以简化这些方法并最终通过链接在一个函数中完成所有事情?
评论太长了。
您在一个函数中混淆了多个问题:
- 展平 ReceiptData
- 驼峰式按键
尚不清楚这些东西是否应该在同一个函数中:虽然它是更短的代码,但这是一种虚假的经济。一旦你通过 minifier + gzip 运行 它,差异可以忽略不计,虽然较短的代码通常更容易阅读,但混淆不同的关注点会使它变得更难。除非你的数据集真的很大(在这种情况下你为什么要在客户端处理它?)对运行时间性能的影响可以忽略不计。
所以我会推荐更多这样的东西:
let data = howeverYouGotTheData;
let flattenReceiptData = o => {
o.ReceiptData = o.ReceiptData.reduce((acc, record) => {
acc[_.camelCase(record.lineNum)] = record.Data;
return acc;
}, {});
return o;
};
let camelCaseKeys = o => {
return Object.entries(o).reduce((acc, [k, v]) => {
acc[_.camelCase(k)] = v;
return acc;
}, {});
};
// _.flow is lodash's implementation of left composition
let flattenAndCase = _.flow(flattenReceiptData, camelCaseKeys);
let result = flattenAndCase(data);
现在关注点被适当地分离了,一切都被清楚地命名并且易于阅读,等等。如果你真的想打高尔夫球,你可以使用像这样的一些标准技巧:
let cc = _.camelCase;
let flattenRD = o => (o.ReceiptData = o.ReceiptData.reduce((a, x) => (a[cc(x.lineNum)] = x.Data, a), {}));
等等。我使用逗号运算符将赋值和 return 压缩为一个表达式,从而避免了对 Object.assign
.