const input = {
'foo.0.first': 111,
'foo.1.first': 222,
'bar.second': 333,
};
const output = {
foo: [{ first: 111 }, { first: 222 }],
bar: { second: 333 }
};
function setVal(obj, path, val) {
const keys = path.split('.');
const key = keys.pop();
keys.reduce((p, c) => p[c] = p[c] || {}, obj)[key] = val;
return obj;
}
function replaceObjWithArr(obj) {
if (obj instanceof Object) {
const keys = Object.keys(obj).sort((a, b) => a - b);
obj = keys.every((n, i) => +n === i) ? keys.map(n => obj[n]) : obj;
keys.forEach(n => obj[n] = replaceObjWithArr(obj[n]));
}
return obj;
}
const output = replaceObjWithArr(Object
.entries(input)
.reduce((acc, n) => setVal(acc, ...n), {})
);
{
᠌ ᠌᠌ ᠌ ᠌ "foo": {
᠌ ᠌᠌ ᠌ ᠌ ᠌ ᠌᠌ ᠌ ᠌ "0": {
᠌ ᠌᠌ ᠌ ᠌ ᠌ ᠌᠌ ᠌ ᠌ ᠌ ᠌᠌ ᠌ ᠌ "first": 111
᠌ ᠌᠌ ᠌ ᠌ ᠌ ᠌᠌ ᠌ ᠌ },
᠌ ᠌᠌ ᠌ ᠌ ᠌ ᠌᠌ ᠌ ᠌ "1": {
᠌ ᠌᠌ ᠌ ᠌ ᠌ ᠌᠌ ᠌ ᠌ ᠌ ᠌᠌ ᠌ ᠌ "first": 222
᠌ ᠌᠌ ᠌ ᠌ ᠌ ᠌᠌ ᠌ ᠌ }
᠌ ᠌᠌ ᠌ ᠌ },
᠌ ᠌᠌ ᠌ ᠌ "bar": {
᠌ ᠌᠌ ᠌ ᠌ ᠌ ᠌᠌ ᠌ ᠌ "second": 333
᠌ ᠌᠌ ᠌ ᠌ }
}
const insert = (target,path,value) => path.split(".").reduce((a,k,i,l)=>a[k]=i+1==l.length?value:a[k]||{},target);
const parse = (list,result={}) => Object.keys(list).forEach(k=>insert(result,k,list[k]))||result;
const output = parse(input);
const getOutput = input => {
let obj = {}
for(key in input) {
let keyArr = [...key.split('.')]
if(obj[keyArr[0]]) {
obj[keyArr[0]].push({[keyArr[keyArr.length - 1]]: input[key]})
} else {
obj[keyArr[0]] = [{[keyArr[keyArr.length - 1]]: input[key]}]
}
}
for(key in obj) {
if(obj[key].length === 1) obj[key] = obj[key][0]
}
return obj
}
function createObject(input) {
let output = {}
Object.keys(input).map((item, idx) => {
let inputValue = input[Object.keys(input)[idx]],
arr = item.split('.'),
[mainKey, subKey] = arr;
if (typeof output[mainKey] === 'undefined') output[mainKey] = arr.length > 2 ? [] : {};
if (Array.isArray(output[mainKey])) {
let obj = new Object();
obj[subKey] = inputValue;
output[mainKey].push(obj);
} else {
output[mainKey][subKey] = inputValue;
}
});
return output;
}