DEV Community

ray_v101
ray_v101

Posted on

is there anyway to submit analogus array values into sub-array?

This is the array that i tried working with :

let array = [
"getX",
"getY",
"getZ",
"getRotationX",
"getRotationY",
"getRotationZ",
"getRotation"];

I tried different ways , but coudn't acheieve so , I want to arrange analogus valuegetx, gety, getz into one subarray, same goes for rotation and scale. Does anyone have idea on how to proceed ?

output: const array = [['getY','getX','getZ'], ['getPositionX'....],...];

Top comments (4)

Collapse
 
dance2die profile image
Sung M. Kim

If your JS environment support Array#flatMap, you can do a simple regex match.

[array.flatMap(f => f.match(/get[XYZ]/g)).filter(Boolean), 
 array.flatMap(f => f.match(/getRotation[XYZ]/g)).filter(Boolean)]

demo

If not, then you'd have to flat the result of regex's match value (as .match returns an array of values)

Collapse
 
gautamraju15 profile image
ray_v101

thankyou :)

Collapse
 
dance2die profile image
Sung M. Kim

You're welcome~

Collapse
 
gautamraju15 profile image
ray_v101

thankyou so much kim :)