1

I have an array of objects, each object MAY contain more arrays of objects and so on. I want to iterate through all these objects without hard coding it. I don't know how many nested arrays will each object have.

// This is the parent array
var members = [Object, Object, Object, Object, Object];

// members[0] can have another array of objects
var member = members[0];

and so on....
var mem = member[0];

I want to iterate through all those objects, but like I said, I can't know how many there will be. I need to get label of every object which I currently get by Object.label I hope I explained it well, here's the picture. Array[5] is var members in my example enter image description here

2
  • So you loop over the array and read the label? And when you encounter the next object, you do recursion and loop over that. Commented Apr 11, 2017 at 12:45
  • 1
    You can use recursion. But this will need a standard object structure Commented Apr 11, 2017 at 12:45

5 Answers 5

3

I would solve this with a recursive function that iterates through all children. Given they all share the same structure.

function visit(o) {
  for (var i = 0; i<o.length; i++) {        
    /* do something useful */
    console.log(o[i].label);

    if (Array.isArray(o[i].children)) {
      visit(o[i].children);
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Got it working. your o in if should be with [ i ]. So if (o[i].children) { visit(o[i].children); }
1

Another way:

function getMembers(list){
  list.forEach(function(item){
    console.log(item.label); 
    if(item.children){
      getMembers(item.children)
    }
  });
}

var members = [{label: 'label1', children:[{label: 'label21'}, {label: 'label22'}]}, {label: 'label2'}, {label: 'label3'}];

getMembers(members);

Comments

0

Try this. Check whether child element is array using Js Array.isArray and if it is array call the function someFn again

function someFn(members) {
var labels=[]
for (var i = 0; i<members.length; i++) { 
    labels.push(members[i].label)

    if (Array.isArray(members[i]) && members[i].length>0) {
      someFn(members[i]);
    }
  }
retun labels;
}
var membersArray = [Object, Object, Object, Object, Object];
var labelsArray= someFn(membersArray);
console.log(labelsArray);

I'll add some demo later

Comments

0

    var jsonArr = [{"children":["New1","New2","New3","New4"]},{"children1":
                  ["New11","New12","New13","New14"],"children2":
                  ["New21","New22","New23","New24"]},{"children2":
                  ["New21","New22","New23","New24"]}];

	function callRecFunc(obj){
		$.each(obj,function(i,o){
			if(o != undefined && o instanceof Array){
				callRecFunc(o);
			}
			else{
				console.log(o);
			}
		});
	}

	$.each(jsonArr,function(ind,obj){
			callRecFunc(obj);
	})
   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

1 Comment

Try out this code, this recursion is work for all the nested arrays and after that it will print array element data. But at the leaf node there must be a simple array with values
0

You can write a function that given an object it will read label property in this object and loop throught children array of this object if it exists.

function readObj(obj){
  console.log(obj.label);
  if(obj.children && obj.children.length>0){
       obj.children.map(readObj);
  }
}

And then apply this function to all members array.

function readObj(obj) {
  console.log(obj.label);
  if (obj.children && obj.children.length > 0) {
    obj.children.map(readObj);
  }
}

var members = [{
  label: "label1"
}, {
  label: "Label2",
  children: [{
    label: "child1"
  }, {
    label: "child2"
  }]
}, {
  label: "label3",
  children: [{
    label: "child3"
  }]
}];

members.map(readObj);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.