I’m building a dynamic xChart. The dynamic data I’m passing is a pre-built string ready to be converted to an object by js:
{"data": [{"x":"car insurance companies","y":1417},
{"x":"insurance companies","y":17201},
{"x":"auto insurance companies","y":892},
{"x":"car insurance quote","y":3280},
{"x":"auto insurance quote","y":988}]}
Here’s a sample snippet of parameter code that xCharts needs:
var data = {
"xScale": "ordinal",
"yScale": "linear",
"main": [
{
"className": ".pizza",
"data": [
{
"x": "Pepperoni",
"y": 4
},
{
"x": "Cheese",
"y": 8
}
]
}
]
};
And here is MY set of parameters:
var vars = {
"xScale": "ordinal",
"yScale": "linear",
"type": "bar",
"main": [
{
"className": ".topsy-results"
}
]
};
I need to add my data object to the main object in my parameter list to make it complete. If I $.parseJSON the data object it gives me an object of objects which will not work. How do I parse the data object to get the format I need (to make it match the sample code I gave)?
Use
$.extendand just merge the two?I’ve placed
prebuiltin to another object so the nesting works out when it’s extended , but nowprebuiltshould show withindataof themergedobject. Note:$.extendis useful if you have more than just the data information or if there are existing pieces in thedataproperty already and you want to either update them or add new ones).You can also simply assign it just by reference: