如何仅转换嵌套对象中的2个属性,其余属性保持不变?
优化json2csv转换钩子,避免冗余代码
你可以通过以下两种方式简化代码,无需手动枚举所有不需要转换的字段:
方法一:修改原对象的指定属性(推荐)
直接基于原数据结构修改需要处理的字段,利用json2csv的路径解析能力自动提取其余字段:
const { Parser: Json2csvParser } = require('json2csv'); const json2csvFields = [ { value: 'root.filename', label: 'File Name' }, { value: 'issue.root.priority', label: 'Priority' }, { value: 'issue.root.url', label: 'URL' }, { value: 'issue.root.startline', label: 'Start Line' }, { value: 'issue.root.stopline', label: 'Stop Line' }, { value: 'issue.root.startcolumn', label: 'Start Column' }, { value: 'issue.root.stopcolumn', label: 'Stop Column' }, { value: 'issue.root.issuename', label: 'Issue Name' }, { value: 'issue.root.issuecategory', label: 'Issue Category' }, { value: 'issue._', label: 'Issue Description' }, ]; const sampleData = [ { root: { filename: '/home/users/john-doe/workspace/foo-project/src/main/classes/foo.cls', }, issue: { root: { priority: 1, url: 'www.example.com', startline: 100, stopline: 105, startcolumn: 20, stopcolumn: 25, issuename: 'blah', issuecategory: 'Category A', }, _: ' Fox ', }, }, ]; // 深拷贝原数据,避免修改原始输入 const deepClone = (obj) => JSON.parse(JSON.stringify(obj)); const json2csvOptions = { fields: json2csvFields, quote: '', header: true, transforms: [ (item) => { const newItem = deepClone(item); // 处理文件名截断 newItem.root.filename = newItem.root.filename.replace( '/home/users/john-doe/workspace/foo-project/src/main/classes/', '' ); // 处理描述字段的修剪和引号包裹 newItem.issue._ = `"${newItem.issue._.trim()}"`; return newItem; }, ], }; const json2csvParser = new Json2csvParser(json2csvOptions); const csv = json2csvParser.parse(sampleData); console.log(csv);
说明:
- 用
deepClone深拷贝原数据,避免污染原始输入 - 仅对需要转换的两个字段做针对性修改,其余字段保持原结构
- 依靠json2csv的路径解析能力(
fields中的value为点路径),自动提取其余字段的值,无需手动枚举
方法二:基于字段列表动态生成转换结果
遍历字段列表,只对目标字段做转换,其余字段通过路径提取:
const { Parser: Json2csvParser } = require('json2csv'); const json2csvFields = [ { value: 'root.filename', label: 'File Name' }, { value: 'issue.root.priority', label: 'Priority' }, { value: 'issue.root.url', label: 'URL' }, { value: 'issue.root.startline', label: 'Start Line' }, { value: 'issue.root.stopline', label: 'Stop Line' }, { value: 'issue.root.startcolumn', label: 'Start Column' }, { value: 'issue.root.stopcolumn', label: 'Stop Column' }, { value: 'issue.root.issuename', label: 'Issue Name' }, { value: 'issue.root.issuecategory', label: 'Issue Category' }, { value: 'issue._', label: 'Issue Description' }, ]; const sampleData = [ { root: { filename: '/home/users/john-doe/workspace/foo-project/src/main/classes/foo.cls', }, issue: { root: { priority: 1, url: 'www.example.com', startline: 100, stopline: 105, startcolumn: 20, stopcolumn: 25, issuename: 'blah', issuecategory: 'Category A', }, _: ' Fox ', }, }, ]; // 根据点路径获取嵌套对象的值 const getNestedValue = (obj, path) => { return path.split('.').reduce((current, key) => current?.[key], obj); }; const json2csvOptions = { fields: json2csvFields, quote: '', header: true, transforms: [ (item) => { const transformed = {}; json2csvFields.forEach(({ value: fieldPath }) => { if (fieldPath === 'root.filename') { transformed[fieldPath] = item.root.filename.replace( '/home/users/john-doe/workspace/foo-project/src/main/classes/', '' ); } else if (fieldPath === 'issue._') { transformed[fieldPath] = `"${item.issue._.trim()}"`; } else { transformed[fieldPath] = getNestedValue(item, fieldPath); } }); return transformed; }, ], }; const json2csvParser = new Json2csvParser(json2csvOptions); const csv = json2csvParser.parse(sampleData); console.log(csv);
说明:
- 自定义
getNestedValue函数,实现从嵌套对象中按点路径提取值 - 遍历字段列表,仅对需要转换的字段做特殊处理,其余字段自动提取
- 适合字段数量较多或需要动态调整字段的场景
两种方法都能输出与原代码完全一致的结果,且彻底消除了冗余的字段枚举。
内容的提问来源于stack exchange,提问作者javanoob
相关产品推荐
相关产品推荐

