[Be Solved]How to find objects , return an array?

Hello everyone!
I created a scene. Object has child object, child object’s name such as car1,car2., so how do I get an array of objects,which has’car’ in its name? I have found the Object3D’s function, such as 'getObjectById ’ 'getObjectByName ’ or 'getObjectByProperty '. They all returned the first with a matching。 I want to konw ,is there a way to get this kind of objects。 Thx!

Not via the library. You would have to write your own method than provides you the respective result. However, I would not utilize Object3D.name for this. Instead, I would add a new property to Object3D.userData (for example tag) and then do this:

THREE.Object3D.prototype.getObjectsByTag = function( tag, result ) {

  // check the current object

  if ( this.userData.tag === tag ) result.push( this );
  
  // check children

	for ( var i = 0, l = this.children.length; i < l; i ++ ) {

        var child = this.children[ i ];

        child.getObjectsByTag( tag, result );

	}
   
  return result;

};

The method is used like so:

var objects = [];
scene.getObjectsByTag( 'car', objects ); // the found objects are written in the given array

Demo: https://jsfiddle.net/71bdmo5z/

3 Likes

Thanks for your guidance.