javascript - How to get two different array objects together -


here attached json .

"mainsteps": [   {     "id": "9b3b64b4-d8a5-46d5-b464-066dc5c45dc3",     "name": "main step 1",     "steps": [       {         "name": "sub step 1.1"       },       {         "name": "sub step 1.2"       }     ]   },   {     "name": "main step 2"     "steps": [       {         "name": "sub step 2.1"       },       {       "name": "sub step 2.2"       }     ],   },   {     "name": "main step 3",     "steps": [       {         "name": "sub step 3.1"       },       {         "name": "sub step 3.2"       }     ],   } ] 

am looking output --> [main step 1, sub step 1.1 , sub step 1.2] ,[main step 2, sub step 2.1 , sub step 2.2] , [main step 3, sub step 3.1 , sub step 3.2] . spend whole day output getting output [[main step 1,main step 2,main step 3,sub step 1.1,sub step 1.2....] getting different format's unable actual output mentioned , can clarify me .

	var dataprocess = {          				parentprocess:[],          				subprocess:[]          		                   };   		                        var steps = maindata.steps; // steps having full json data                             		var proc = [];                   $scope.getsteps = function(steps) {                      (var i=0;i< steps.length;i++) {                  	       dataprocess.parentprocess.push(steps[i].name);                  	     for(var j=i;j<steps[i].steps.length;j++){                  		  dataprocess.subprocess.push(steps[i].steps[j].name);                  	   }                     }

this 1 of way tried ,

if need es5 syntax:

var details = mainsteps.map(function(step) {   return [ step.name ].concat((step.steps || []).map(function(substep){     return substep.name;   }) }); 

es6 syntax:

var details = mainsteps.map(step =< [step.name].concat((step.steps || []).map(sub => sub.name)); 

if need more recursion 1 layer deep, can use function top level mapper, calls itself.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -