I am trying to transform data using datascript function.but I am not able to add children in array of it's parent.
var a ={ "GENERAL_INFORMATION": { "value": null, "type": "LABEL", "editable": false, "dataType": null, "required": false, "displayName": null, "pattern": null }, " NUMBER": { "value": "9876834940", "type": "FIELD", "editable": false, "dataType": "", "required": false, "displayName": null, "pattern": null }, "CON TYPE": { "value": "retailnn_cyn", "type": "FIELD", "editable": false, "dataType": "", "required": false, "displayName": null, "pattern": null }, "User INFORMATION": { "value": null, "type": "LABEL", "editable": false, "dataType": null, "required": false, "displayName": null, "pattern": null }, "Title": { "value": "Mr", "type": "FIELD", "editable": true, "dataType": "", "required": true, "displayName": null, "pattern": null }, "Gender": { "value": "M", "type": "FIELD", "editable": true, "dataType": "", "required": true, "displayName": null, "pattern": null }, "DOB": { "value": "23-Oct-1984", "type": "FIELD", "editable": true, "dataType": "DATE", "required": true, "displayName": null, "pattern": null } }; var o = []; for (var i in a){ if(a[i].type==='LABEL'){ a[i].text = i; a[i].children = [] o.push(a[i]) }else if(a[i].type==='FIELD'){ } } getting output;
[{ "value": null, "type": "LABEL", "editable": false, "dataType": null, "required": false, "displayName": null, "pattern": null, "text": "GENERAL_INFORMATION", "children": [] }, { "value": null, "type": "LABEL", "editable": false, "dataType": null, "required": false, "displayName": null, "pattern": null, "text": "User INFORMATION", "children": [] }] Expected output
[{ "value": null, "type": "LABEL", "editable": false, "dataType": null, "required": false, "displayName": null, "pattern": null, "text": "GENERAL_INFORMATION", "children": [ { "value": "9876834940", "type": "FIELD", "editable": false, "dataType": "", "required": false, "displayName": null, "pattern": null, "text": "NUMBER", }, { "value": "retailnn_cyn", "type": "FIELD", "editable": false, "dataType": "", "required": false, "displayName": null, "pattern": null, "text": "CON TYPE", } ] }, { "value": null, "type": "LABEL", "editable": false, "dataType": null, "required": false, "displayName": null, "pattern": null, "text": "User INFORMATION", "children": [ { "value": "Mr", "type": "FIELD", "editable": true, "dataType": "", "required": true, "displayName": null, "pattern": null, "text": "Title", }, { "value": "M", "type": "FIELD", "editable": true, "dataType": "", "required": true, "displayName": null, "pattern": null, "text": "Gender", }, { "value": "23-Oct-1984", "type": "FIELD", "editable": true, "dataType": "", "required": true, "displayName": null, "pattern": null, "text": "DOB", } ] }] console.log(JSON.stringify(o)) here is my code https://jsbin.com/wofufuriqe/2/edit?html,js,console
type: 'FIELD') coming aftertype: 'LABEL'should become children of the previoustype: 'LABEL'element. Is that so?