I have an Object
x = {
"y": 55.11,
"color": "#4572A7",
"level": 0,
"drilldown": {
"name": "MSIE versions",
"level": 1,
"color": "#4572A7",
"categories": ["MSIE 8.0", "MSIE 6.0", "MSIE 7.0", "MSIE 9.0"],
"data": [
{
"y": 33.06,
"color": "#4572A7",
"level": 1,
"drilldown": {
"name": "drilldown next level",
"level": 2,
"color": "#4572A7",
"categories": ["a", "b", "c"],
"data": [23,54,47]
}
},
]
}
}
Note this could actually go in any depth as I am adding new properties to the object but is speific format like
x['drilldown] = {}
x.drilldown['data] =[]
....and so on....
So suppose at a time I have below object structure:
x {
drilldown {
data [
drilldown {
data [1,2,3]
}
]
}
}
.....................
I need to go to the second last ‘data’ element of object ‘x’ and push new values. So based upon my example above it would be something like this:
x.drilldown.data.push(10);
So in all I need to find the second last data property in object ‘x’ and then push value in it
x.second last data array.push(10);
How can I do that?
==================================================================================
[Added Solution]
I added the below code and it works fine. I hope it is the right way to-do:
var data = [10,11,12];
var datatemp = x.drilldown;
var datatempvar = {};
for (;typeof datatemp != 'undefined';)
{
datatempvar = datatemp;
datatemp = datatemp.drilldown;
}
for(it=0;it<data.length; it++)
datatempvar.data.push(data[it]);
Thanks everyone – this forum helped me a lot.
What should happen if a data array contains more than one value, especially some objects-with-drilldowns and some plain values?
In every case you will have to set up a loop, which tests whether there are still drilldowns two levels further and does this for every level. When it breaks, the current level is the one where you want to add your elements.