Open Rain120 opened 2 years ago
var deFlatten = function (entry) {
const res = {};
var format = (res, keys, value) => {
const key = keys.shift();
if (!keys.length) {
res[key] = value;
} else {
res[key] = res[key] || {};
format(res[key], keys, value);
}
};
for (const key in entry) {
const keys = key.split('.');
format(res, keys, entry[key]);
}
for (const key in res) {
if (/^\w+\[\d+\]$/.test(key)) {
const k = key.match(/^\w+/)[0];
const i = key.match(/[\d+]/g).join('');
res[k] = res[k] || [];
res[k][i] = res[key];
delete res[key];
}
}
return res;
}
deFlatten({
"a.b": 1,
"a.c": 2,
"asss.d.e": 5,
"baa[0]": 1,
"b[1]": 3,
"b[2].a": 2,
"b[20].b": 3,
"c": 3
})