Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access dynamically named JavaScript Objects?

I have a JS Object:

var testObj = new Object();

that has a property called volume1, so

testObj.volume1

has a value. How would I go about accessing that property by using something along the lines of

testObj.volume + "1"

To give it more context, the object actually has more properties, like

testObj.volume1, testObj.volume2 and testObj.volume3 

and I want to access them using some iteration. I've tried playing with the window[] element, but have had not much success.

Thanks!

like image 234
Rio Avatar asked Feb 01 '26 14:02

Rio


1 Answers

   testObj["volume" + 1]

but you actually want an array here

   testObj.volume = [...]
   testObj.volume[1] = whatever
like image 196
user187291 Avatar answered Feb 04 '26 03:02

user187291



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!