Suppose i have this json
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
I Have the list of those objects and puting in variable like this
var myobjects = data
which contains list of above objects
now that is working fine butnow i have one problem. Instead of directly getting list of objects, i now have one parent object which contain those object liek this
"id": "21"
"myobject":
{
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
}
instead of getting list of childobjects , i ow have the list of parent objects in data
how can get the list of child objects from list of parent objects like before
something like
var childobjects = data.childobjects
I want that because i ahve templates coded which only wok if i have the direct list of objects
Do you want to flatten the array of objects?
// suppose data is like this:
var data = [
{ id: 1, myobject: {widgets:'',whatever:''}},
{ id: 5, myobject: {widgets:'',whatever:''}},
{ id: 23, myobject: {widgets:'',whatever:''}}
]
Then this code:
var myobjects = [];
for (key in data) {
myobjects.push(data[key].myobject);
}
Will leave myobjects
equal to:
[
{widgets:'',whatever:''},
{widgets:'',whatever:''},
{widgets:'',whatever:''}
]
I think it's what you need:
<script type="text/javascript">
var parents = [{"id": "21",
"myobject":
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}
}},
{"id": "22",
"myobject":
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}
}}
];
for(x in parents ){
console.log(parents[x].myobject);
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.