I have the following JSON object:
myJsonObj = {
"name":"Laptop",
"type":"hardware computer laptop",
"forward":[
{
"name":"Depends On",
"forward":[
{
"name":"test asset 1",
"type":"hardware",
"link":"somelink"
}
]
},
{
"name":"somename",
"forward":[
{
"name":"test asset 5",
"type":"hardware",
"link":"somelink"
}
]
}
],
"inverse":[
{
"name":"somename",
"inverse":[
{
"name":"test asset 4",
"ciTypeCls":"hardware",
"link":"somelink"
},
{
"name":"test asset 1",
"ciTypeCls":"hardware",
"link":"somelink"
}
]
},
{
"name":"somename",
"inverse":[
{
"name":"test asset 1",
"ciTypeCls":"hardware",
"link":"somelink"
}
]
}
]
}
In an attempt to change the property name forward
to children
, below is my code:
myJsonObj = {
"name": "Laptop",
"type": "hardware computer laptop",
"forward": [{
"name": "Depends On",
"forward": [{
"name": "test asset 1",
"type": "hardware",
"link": "somelink"
}]
}, {
"name": "somename",
"forward": [{
"name": "test asset 5",
"type": "hardware",
"link": "somelink"
}]
}],
"inverse": [{
"name": "somename",
"inverse": [{
"name": "test asset 4",
"ciTypeCls": "hardware",
"link": "somelink"
}, {
"name": "test asset 1",
"ciTypeCls": "hardware",
"link": "somelink"
}]
}, {
"name": "somename",
"inverse": [{
"name": "test asset 1",
"ciTypeCls": "hardware",
"link": "somelink"
}]
}]
}
jQuery.each(myJsonObj, function(e) {
e.children = e.forward;
delete e.forward;
});
console.log(myJsonObj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
However, it does not seem to work. What am I doing wrong?