1
我正在接收JSON数组,将其展平为将其显示为表,但是我使用的这个逻辑有一个缺陷,如果我的对象具有相同的列名,它将忽略它,添加找到的第一列。如何将表名添加到表列
问题我想解决
显示嵌套对象为平表
问题我停留在
我想对象的名称添加到列标题,这是目前没有发生。
http://image.prntscr.com/image/55ef0e49abd748d19b9f8c88bd5a8652.png
我认为问题是在这里
function flattenRecord(result, rec) {
return Object.keys(rec).reduce(function(result, key) {
var value = rec[key];
if (value && typeof value === 'object')
flattenRecord(result, value);
else
result[key] = value; // How to add key here so that it will be added to column header ?
return result;
}, result);
}
完整的代码和工作实例
https://jsfiddle.net/1tsu6xt9/14/
我猜你正在寻找一种方式来拉平和unflatten对象。 [喜欢这个图书馆](https://github.com/hughsk/flat)。 – Sheepy
https://jsfiddle.net/1tsu6xt9/20/ Line#56 Lol:D – cswl
@cswl我的JSON数据是动态的,所以我不能硬编码密钥,否则会很容易;) – Mathematics