I want to transform such object from:
foo = {
42: 'foo',
'a.b.c[0].42': 'bar',
'a.b.c[0].43': 'zet',
'a.d.c[0].42': 'baz'
}
To:
bar = {
42: 'foo',
'a.b.c[0].42': 'bar',
'a.b.c[0].43': 'zet',
'a.d.c[0].42': 'baz',
a: {
b: {
c: [{
42: 'bar', 43: 'zet'
}]
},
d: {
c: [{
42: 'baz'
}]
}
}
}
Do anybody know how to implement convertToTree
function?
We use lodash
in my project so that can help with basic operations.
var object = {
42: "foo",
"a.b.c[0].42": "bar",
"a.b.c[0].43": "zet",
"a.d.c[0].42": "baz"
};
function convertToTree() {
// code here
}
convertToTree(object) === {
"42": "foo",
"a.b.c[0].42": "bar",
"a.b.c[0].43": "zet",
"a.d.c[0].42": "baz",
"a": {
"b": {
"c": [
{
"42": "bar",
"43": "zet"
}
]
},
"d": {
"c": [
{
"42": "baz"
}
]
}
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
UPD:
I need this transformation for doing such operation in another place of code:
_.result(bar, 'a.b.c[0].42') === 'bar'
You can find _.result
function description here.