我将一些代码放在一起,以平抑和反平抑复杂/嵌套的JavaScript对象。它可以工作,但有点慢(触发“长脚本”警告)。
对于扁平的名称,我希望用“.”作为分隔符,用[INDEX]作为数组。
例子:
un-flattened | flattened
---------------------------
{foo:{bar:false}} => {"foo.bar":false}
{a:[{b:["c","d"]}]} => {"a[0].b[0]":"c","a[0].b[1]":"d"}
[1,[2,[3,4],5],6] => {"[0]":1,"[1].[0]":2,"[1].[1].[0]":3,"[1].[1].[1]":4,"[1].[2]":5,"[2]":6}
我创建了一个基准测试,用于模拟我的用例http://jsfiddle.net/WSzec/
获得一个嵌套对象
压平它
查看它,并可能修改它,而扁平
将其平放回原始的嵌套格式,然后运走
我想要更快的代码:为了澄清,代码完成JSFiddle基准测试(http://jsfiddle.net/WSzec/)显著更快(~20%+会很好)在IE 9+, FF 24+和Chrome 29+。
以下是相关JavaScript代码:当前最快速度:http://jsfiddle.net/WSzec/6/
var unflatten = function(data) {
"use strict";
if (Object(data) !== data || Array.isArray(data))
return data;
var result = {}, cur, prop, idx, last, temp;
for(var p in data) {
cur = result, prop = "", last = 0;
do {
idx = p.indexOf(".", last);
temp = p.substring(last, idx !== -1 ? idx : undefined);
cur = cur[prop] || (cur[prop] = (!isNaN(parseInt(temp)) ? [] : {}));
prop = temp;
last = idx + 1;
} while(idx >= 0);
cur[prop] = data[p];
}
return result[""];
}
var flatten = function(data) {
var result = {};
function recurse (cur, prop) {
if (Object(cur) !== cur) {
result[prop] = cur;
} else if (Array.isArray(cur)) {
for(var i=0, l=cur.length; i<l; i++)
recurse(cur[i], prop ? prop+"."+i : ""+i);
if (l == 0)
result[prop] = [];
} else {
var isEmpty = true;
for (var p in cur) {
isEmpty = false;
recurse(cur[p], prop ? prop+"."+p : p);
}
if (isEmpty)
result[prop] = {};
}
}
recurse(data, "");
return result;
}
编辑1修改了上面的@Bergi的实现,这是目前最快的。顺便说一句,使用“。”用indexOf代替正则表达式。exec”在FF中快了20%左右,但在Chrome中慢了20%;所以我将坚持使用正则表达式,因为它更简单(这里是我尝试使用indexOf来取代正则表达式http://jsfiddle.net/WSzec/2/)。
在@Bergi的想法的基础上,我设法创建了一个更快的非正则表达式版本(在FF快3倍,在Chrome快10%)。http://jsfiddle.net/WSzec/6/在这个(当前)实现中,密钥名称的规则很简单,密钥不能以整数开头或包含句点。
例子:
{"foo":{"bar":[0]}} => {"foo.bar.0":0}
EDIT 3添加@AaditMShah的内联路径解析方法(而不是String.split)有助于提高unflatten性能。我对整体性能的提升非常满意。
最新版本的jsfiddle和jsperf:
http://jsfiddle.net/WSzec/14/
http://jsperf.com/flatten-un-flatten/4
三年半后……
对于我自己的项目,我想在mongoDB点表示法中平坦JSON对象,并提出了一个简单的解决方案:
/**
* Recursively flattens a JSON object using dot notation.
*
* NOTE: input must be an object as described by JSON spec. Arbitrary
* JS objects (e.g. {a: () => 42}) may result in unexpected output.
* MOREOVER, it removes keys with empty objects/arrays as value (see
* examples bellow).
*
* @example
* // returns {a:1, 'b.0.c': 2, 'b.0.d.e': 3, 'b.1': 4}
* flatten({a: 1, b: [{c: 2, d: {e: 3}}, 4]})
* // returns {a:1, 'b.0.c': 2, 'b.0.d.e.0': true, 'b.0.d.e.1': false, 'b.0.d.e.2.f': 1}
* flatten({a: 1, b: [{c: 2, d: {e: [true, false, {f: 1}]}}]})
* // return {a: 1}
* flatten({a: 1, b: [], c: {}})
*
* @param obj item to be flattened
* @param {Array.string} [prefix=[]] chain of prefix joined with a dot and prepended to key
* @param {Object} [current={}] result of flatten during the recursion
*
* @see https://docs.mongodb.com/manual/core/document/#dot-notation
*/
function flatten (obj, prefix, current) {
prefix = prefix || []
current = current || {}
// Remember kids, null is also an object!
if (typeof (obj) === 'object' && obj !== null) {
Object.keys(obj).forEach(key => {
this.flatten(obj[key], prefix.concat(key), current)
})
} else {
current[prefix.join('.')] = obj
}
return current
}
特性和/或注意事项
它只接受JSON对象。因此,如果你传递类似{a:() =>{}}的东西,你可能得不到你想要的!
它删除空数组和对象。所以这个{a: {}, b:[]}被平化为{}。
这是我的。它在谷歌应用程序脚本中在一个相当大的对象上运行<2ms。它使用破折号而不是点作为分隔符,并且它不像提问者的问题中那样专门处理数组,但这是我想要的。
function flatten (obj) {
var newObj = {};
for (var key in obj) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
var temp = flatten(obj[key])
for (var key2 in temp) {
newObj[key+"-"+key2] = temp[key2];
}
} else {
newObj[key] = obj[key];
}
}
return newObj;
}
例子:
var test = {
a: 1,
b: 2,
c: {
c1: 3.1,
c2: 3.2
},
d: 4,
e: {
e1: 5.1,
e2: 5.2,
e3: {
e3a: 5.31,
e3b: 5.32
},
e4: 5.4
},
f: 6
}
Logger.log("start");
Logger.log(JSON.stringify(flatten(test),null,2));
Logger.log("done");
示例输出:
[17-02-08 13:21:05:245 CST] start
[17-02-08 13:21:05:246 CST] {
"a": 1,
"b": 2,
"c-c1": 3.1,
"c-c2": 3.2,
"d": 4,
"e-e1": 5.1,
"e-e2": 5.2,
"e-e3-e3a": 5.31,
"e-e3-e3b": 5.32,
"e-e4": 5.4,
"f": 6
}
[17-02-08 13:21:05:247 CST] done
我想要一种方法,这样我就可以轻松地将我的json数据转换为csv文件。
这个场景是:我从某个地方查询数据,然后收到某个模型的数组,比如银行提取。
下面的方法用于解析这些条目中的每一个。
function jsonFlatter(data, previousKey, obj) {
obj = obj || {}
previousKey = previousKey || ""
Object.keys(data).map(key => {
let newKey = `${previousKey}${previousKey ? "_" : ""}${key}`
let _value = data[key]
let isArray = Array.isArray(_value)
if (typeof _value !== "object" || isArray || _value == null) {
if (isArray) {
_value = JSON.stringify(_value)
} else if (_value == null) {
_value = "null"
}
obj[newKey] = _value
} else if (typeof _value === "object") {
if (!Object.keys(_value).length) {
obj[newKey] = "null"
} else {
return jsonFlatter(_value, newKey, obj)
}
}
})
return obj
}
这样,我可以依靠对象模型的键和内键的一致性,但是数组只是字符串化的,因为我不能依赖它们的一致性。此外,空对象变成字符串“null”,因为我仍然希望它的关键出现在最终结果中。
使用的例子:
const test_data = {
a: {
aa: {
aaa: 4354,
aab: 654
},
ab: 123
},
b: 234,
c: {},
d: []
}
console.log('result', jsonFlatter(test_data))
#### output
{
"a_aa_aaa": 4354,
"a_aa_aab": 654,
"a_ab": 123,
"b": 234,
"c": "null",
"d": "[]"
}