# Traverse groups function seems to be stuck on the first object without increasing the array or returning anything. Eventually it hits the recursion limit and errors

**URL:** <https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810>\
**Category:** Questions\
**Tags:** traverse\
**Created:** [March 7, 2023, 7:00pm UTC](https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810 "2023-03-07T19:00:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JackGreenEarth](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/jackgreenearth/32/32845_2.png) [@JackGreenEarth](https://discourse.threejs.org/u/JackGreenEarth)\
**Post date:** [March 7, 2023, 7:00pm UTC](https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810/1 "2023-03-07T19:00:32Z")

</div>

```javascript
function traverseGroups(group) {
	let allObjects = [];
	group.traverse(function(object) {
		console.log(allObjects);
		console.log(object);
		if(object.type === 'Group') {
			allObjects.push(...traverseGroups(object));
		} else {
			allObjects.push(object);
		};
	});
	return(allObjects);
};

```

I tried to create an array, then traverse through the group, appending the traverseGroup of the childObject to the array if the childObject is a group, otherwise simply the appending the childObject to the array.

This doesn’t seem to be working, as with the console.log statements I can see that the array seems to remain empty and the object seems to be the same every time (`Object { isObject3D: true, uuid: "bd88a831-bd88-4d88-acfc-dff9118f960f", name: "Scene", type: "Group", parent: {…}, children: (13) […], up: {…}, position: {…}, rotation: {…}, quaternion: {…}, … }`).

I already asked Bing about this, and I followed a couple of it’s suggestions, such as using === rather than ==, and .push() rather than .concat()

Can you please help?

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [March 7, 2023, 7:26pm UTC](https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810/2 "2023-03-07T19:26:00Z")

</div>

Your observation that the same object is printed in the console is correct.

Here is a small hint:

According to the documentation of [`Object3D.traverse`](https://threejs.org/docs/#api/en/core/Object3D.traverse), it executes the callback **on the provided object** and all descendants. So, if you call `traverseGroups` with a group, the first value of parameter `object` will be the same group … and this will push the execution into infinite loop … until the stack gives up.

I hope you can see how to fix your code (i.e. I do not want to steal from you the joy of fixing your own code). However, let me know if you need more help.

---

<div class="post-metadata">

**Author:** ![JackGreenEarth](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/jackgreenearth/32/32845_2.png) [@JackGreenEarth](https://discourse.threejs.org/u/JackGreenEarth)\
**Post date:** [March 7, 2023, 7:53pm UTC](https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810/3 "2023-03-07T19:53:51Z")

</div>

Is there a function that only does it on the descendents, not the current object?

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [March 7, 2023, 7:56pm UTC](https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810/4 "2023-03-07T19:56:22Z")

</div>

Here is one solution using your style. Does it do what you want?

```javascript
function traverseGroups(group) {
	let allObjects = [];
	group.traverse( function(object) {
		if( object.type !== 'Group' )
			allObjects.push(object);
	} );
	return allObjects;
}

```

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [March 7, 2023, 8:04pm UTC](https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810/5 "2023-03-07T20:04:46Z")

</div>

> [@JackGreenEarth](#):
>
> Is there a function that only does it on the descendents, not the current object?

You can loop through all immediate descendents of `object` with a loop:

```javascript
for (let child of object.children) {
   // do whatever you want with child
}

```

---

<div class="post-metadata">

**Author:** ![JackGreenEarth](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/jackgreenearth/32/32845_2.png) [@JackGreenEarth](https://discourse.threejs.org/u/JackGreenEarth)\
**Post date:** [March 7, 2023, 8:11pm UTC](https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810/6 "2023-03-07T20:11:45Z")

</div>

No, because I want it to go inside groups and return all the individual elements inside them.

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [March 7, 2023, 8:18pm UTC](https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810/7 "2023-03-07T20:18:55Z")

</div>

Have you tried it?

---

<div class="post-metadata">

**Author:** ![Lawrence3DPK](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/lawrence3dpk/32/29184_2.png) [@Lawrence3DPK](https://discourse.threejs.org/u/Lawrence3DPK)\
**Post date:** [March 7, 2023, 9:34pm UTC](https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810/8 "2023-03-07T21:34:15Z")

</div>

I don’t get why you’re not just using…

```javascript
function traverseGroups(group) {
	let allObjects = [];
	group.traverse(function(object) {
             allObjects.push(object);
	});
	return(allObjects);
};

```

Won’t this just push all descendants, group or not, to the allObjects array?

---

<div class="post-metadata">

**Author:** ![JackGreenEarth](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/jackgreenearth/32/32845_2.png) [@JackGreenEarth](https://discourse.threejs.org/u/JackGreenEarth)\
**Post date:** [March 7, 2023, 9:44pm UTC](https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810/9 "2023-03-07T21:44:19Z")

</div>

Yeah I tried that it’s working now, thanks!

---

<div class="post-metadata">

**Author:** ![JackGreenEarth](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/jackgreenearth/32/32845_2.png) [@JackGreenEarth](https://discourse.threejs.org/u/JackGreenEarth)\
**Post date:** [March 7, 2023, 9:44pm UTC](https://discourse.threejs.org/t/traverse-groups-function-seems-to-be-stuck-on-the-first-object-without-increasing-the-array-or-returning-anything-eventually-it-hits-the-recursion-limit-and-errors/48810/10 "2023-03-07T21:44:58Z")

</div>

Not in arrays in arrays?
