【笔记】nodejs读取JSON,数组转树
const fs = require('fs'); // --------------- 读取源文件 --------------- const originData = require('./vuxArea3.json'); // --------------- 常量 --------------- const constant = { ARRAY_TYPE: 'Array', JSON_TYPE: 'Json', }; // --------------- 转换的配置 --------------- const config = { originKey: { children: null, parent: 'parent', value: 'value', label: 'name' }, originType: constant.ARRAY_TYPE, targetKey: { children: 'children', parent: null, value: 'value', label: 'label' }, targetType: constant.JSON_TYPE }; // --------------- 转换函数 --------------- function convertArrayToJson(initData, { originKey, targetKey }) { if (!initData || initData.length === 0) return []; if (!initData.length === 1) return initData; if (!(initData instanceof Array)) return [initData]; const result = []; const tempMap = []; const key = originKey.value; const parentKey = originKey.parent; const l = initData.length; let i; // key转换 const data = initData.map(item => ({ [originKey.parent]: item[originKey.parent], [targetKey.value]: item[originKey.value], [targetKey.label]: item[originKey.label] })); // 临时引用 for (i = 0; i < l; i += 1) { tempMap[data[i][key]] = data[i]; } // 叶子通过临时引用挂载,利用所有的节点都是单例,才可以通过临时引用去挂载叶子 // 根元素直接push到根节点 for (i = 0; i < l; i += 1) { const parentValue = data[i][parentKey]; // 当前元素存在父元素,并且子不等于父 if (tempMap[parentValue] && data[i][key] !== parentValue) { // 父元素上在children上加上当前元素 tempMap[parentValue][targetKey.children] = tempMap[parentValue][targetKey.children] || []; tempMap[parentValue][targetKey.children].push(data[i]); } else { // 当前元素是根元素 result.push(data[i]); } } return result; } function convert(data, { originKey, originType, targetKey, targetType }) { if (originType === constant.ARRAY_TYPE && targetType === constant.JSON_TYPE) { return convertArrayToJson(data, { originKey, targetKey }); } return []; } // --------------- 执行 --------------- const result = convert(originData, config); let resultStr = ''; try { resultStr = JSON.stringify(result); } catch (e) { console.log(e); } resultStr = resultStr.replace(/"parent":"[a-zA-Z\d]+",/g, ''); // --------------- 写入 --------------- fs.writeFile('./result.json', resultStr, 'utf-8', (err) => { if (err) { console.log(err); } else { console.log(`### conver ${config.originType} to ${config.targetType} success! `); } });
const fs = require('fs');
// --------------- 读取源文件 ---------------
const originData = require('./vuxArea3.json');
// --------------- 常量 ---------------
const constant = {
ARRAY_TYPE: 'Array',
JSON_TYPE: 'Json',
};
// --------------- 转换的配置 ---------------
const config = {
originKey: {
children: null,
parent: 'parent',
value: 'value',
label: 'name'
},
originType: constant.ARRAY_TYPE,
targetKey: {
children: 'children',
parent: null,
value: 'value',
label: 'label'
},
targetType: constant.JSON_TYPE
};
// --------------- 转换函数 ---------------
function convertArrayToJson(initData, { originKey, targetKey }) {
if (!initData || initData.length === 0) return [];
if (!initData.length === 1) return initData;
if (!(initData instanceof Array)) return [initData];
const result = [];
const tempMap = [];
const key = originKey.value;
const parentKey = originKey.parent;
const l = initData.length;
let i;
// key转换
const data = initData.map(item => ({
[originKey.parent]: item[originKey.parent],
[targetKey.value]: item[originKey.value],
[targetKey.label]: item[originKey.label]
}));
// 临时引用
for (i = 0; i < l; i += 1) {
tempMap[data[i][key]] = data[i];
}
// 叶子通过临时引用挂载,利用所有的节点都是单例,才可以通过临时引用去挂载叶子
// 根元素直接push到根节点
for (i = 0; i < l; i += 1) {
const parentValue = data[i][parentKey];
// 当前元素存在父元素,并且子不等于父
if (tempMap[parentValue] && data[i][key] !== parentValue) {
// 父元素上在children上加上当前元素
tempMap[parentValue][targetKey.children] =
tempMap[parentValue][targetKey.children] || [];
tempMap[parentValue][targetKey.children].push(data[i]);
} else {
// 当前元素是根元素
result.push(data[i]);
}
}
return result;
}
function convert(data, { originKey, originType, targetKey, targetType }) {
if (originType === constant.ARRAY_TYPE && targetType === constant.JSON_TYPE) {
return convertArrayToJson(data, { originKey, targetKey });
}
return [];
}
// --------------- 执行 ---------------
const result = convert(originData, config);
let resultStr = '';
try {
resultStr = JSON.stringify(result);
} catch (e) {
console.log(e);
}
resultStr = resultStr.replace(/"parent":"[a-zA-Z\d]+",/g, '');
// --------------- 写入 ---------------
fs.writeFile('./result.json', resultStr, 'utf-8', (err) => {
if (err) {
console.log(err);
} else {
console.log(`### conver ${config.originType} to ${config.targetType} success! `);
}
});