如何将嵌套对象数组转换为 CSV? [英] How to convert array of nested objects to CSV?
问题描述
我有一个包含嵌套对象的数组,例如这个:
<预><代码>[{"name": "1", "children": [{"name": "1.1", "children":"1.2"}]},{"id": "2", "thing": [{"name": "2.1", "children":"2.2"}]},{"name": "3", "stuff": [{"name": "3.1", "children":"3.2"}]},]对象可以包含不同类型的值,包括其他嵌套对象.
我想将此数组转换为 CSV 格式.
我尝试使用 for .. in
循环、常规嵌套的 for
循环、.map()
和递归进行迭代.不过,我认为递归可能是解决这个特定问题的唯一方法.对于 CSV 字段名称,我想使用导致该值的键序列.
对于给定的示例,我要查找的 CSV 结果是:
name、children.name、children.children、id、thing.name、thing.children、stuff.name、stuff.children1, 1.1, 1.2,,,,2,2.1,2.23,,,,3,3.1,3.2
您可以使用这个 ES6 函数来创建您正在寻找的二维数组,然后您可以轻松地将其转换为 CSV:
function pivot(arr) {var mp = 新地图();函数 setValue(a, path, val) {if (Object(val) !== val) {//原始值var pathStr = path.join('.');var i = (mp.has(pathStr) ? mp : mp.set(pathStr, mp.size)).get(pathStr);a[i] = val;} 别的 {for (var key in val) {setValue(a, key == '0' ? path : path.concat(key), val[key]);}}返回一个;}var 结果 = arr.map( obj => setValue([], [], obj) );返回 [[...mp.keys()], ...result];}函数 toCsv(arr) {返回 arr.map( 行 =>row.map ( val => isNaN(val) ? JSON.stringify(val) : +val ).join(',')).join('\n');}//样本数据无功 arr = [{"name": "1", "children": [{"name": "1.1", "children":"1.2"}]},{"id": "2", "thing": [{"name": "2.1", "children":"2.2"}]},{"name": "3", "stuff": [{"name": "3.1", "children":"3.2"}]},];//转换为二维数组,然后转换为 CSV:console.log(toCsv(pivot(arr)));
.as-console-wrapper { max-height: 100% !important;顶部:0;}
有关将 2D 数组转换为 CSV 的其他方法,请参阅此 问答.
I have an array with nested objects, such as this one:
[
{"name": "1", "children": [{"name": "1.1", "children":"1.2"}]},
{"id": "2", "thing": [{"name": "2.1", "children":"2.2"}]},
{"name": "3", "stuff": [{"name": "3.1", "children":"3.2"}]},
]
The objects can contain values of different types, including other, nested objects.
I want to convert this array to CSV format.
I've tried to iterate with for .. in
loops, regular nested for
loops, .map()
and recursion. I think recursion might be the only way to solve this particular problem, though. For the CSV field names I want to use the sequence of keys that lead to the value.
For the given example, the CSV result I'm looking for is:
name, children.name, children.children,id, thing.name, thing.children, stuff.name, stuff.children
1, 1.1, 1.2,
,,,2,2.1,2.2
3,,,,3,3.1,3.2
You could use this ES6 function to create the 2D array you are looking for, which you can then easily transform to CSV:
function pivot(arr) {
var mp = new Map();
function setValue(a, path, val) {
if (Object(val) !== val) { // primitive value
var pathStr = path.join('.');
var i = (mp.has(pathStr) ? mp : mp.set(pathStr, mp.size)).get(pathStr);
a[i] = val;
} else {
for (var key in val) {
setValue(a, key == '0' ? path : path.concat(key), val[key]);
}
}
return a;
}
var result = arr.map( obj => setValue([], [], obj) );
return [[...mp.keys()], ...result];
}
function toCsv(arr) {
return arr.map( row =>
row.map ( val => isNaN(val) ? JSON.stringify(val) : +val ).join(',')
).join('\n');
}
// Sample data
var arr = [
{"name": "1", "children": [{"name": "1.1", "children":"1.2"}]},
{"id": "2", "thing": [{"name": "2.1", "children":"2.2"}]},
{"name": "3", "stuff": [{"name": "3.1", "children":"3.2"}]},
];
// Conversion to 2D array and then to CSV:
console.log(toCsv(pivot(arr)));
.as-console-wrapper { max-height: 100% !important; top: 0; }
For other ways to convert a 2D array to CSV, see this Q&A.
这篇关于如何将嵌套对象数组转换为 CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!