How to travel into n dimensional object array in Javascript? -
i have object array n dimensions n unknown. here example of data :
data = [{"value":{"id":"1","text":"abcd","parent":""},"children":[{"value":{"id":"2","text":"abcd","parent":"1"},"children":[{"value":{"id":"3","text":"abcd","parent":"1"},"children":[{"value":...
i'd travel inside array read object data. how in javascript?
use recursion, assuming want value use
var data = [ { "value": { "id": "1", "text": "abcd", "parent": "" }, "children": [ { "value": { "id": "2", "text": "abcd", "parent": "1" }, "children": [ { "value": { "id": "3", "text": "abcd", "parent": "1" } } ] } ] } ]; function travel(data){ for(var =0;i< data.length;++i){ if(data[i].hasownproperty('value')){ console.log('value: ',data[i].value); } if(data[i].hasownproperty('children')){ travel(data[i].children); } } } function collect(data) { var res= []; function travelaux(data){ for(var =0;i< data.length;++i){ if(data[i].hasownproperty('value')){ res.push(data[i].value); } if(data[i].hasownproperty('children')){ travelaux(data[i].children); } } } travelaux(data); return res; } travel(data);
Comments
Post a Comment