espace Posted April 10, 2017 Share Posted April 10, 2017 hi, i would concatenate a variable and also make a loop. How do you do that ? i receive the error : Invalid left-hand side in assignment thanks for your help. var check_storage=function(){ //level_number=5 for (var i = 0; i < 2; i++){ 'canon'+level_number+i=0 console.log('canon'+level_number+i) } } Link to comment Share on other sites More sharing options...
espace Posted April 10, 2017 Author Share Posted April 10, 2017 var check_storage=function(_create_object){ var 'canon_local'+level_number={} //level_number=5 console.log('canon_local'+level_number) for (var i = 0; i < 2; i++){ 'canon'+level_number[i]="something" console.log('canon'+level_number[i]) } } try also this but that don't work Link to comment Share on other sites More sharing options...
espace Posted April 10, 2017 Author Share Posted April 10, 2017 in fact my problem is simple how do this var "myvariable"+i="something" Link to comment Share on other sites More sharing options...
spinnerbox Posted April 10, 2017 Share Posted April 10, 2017 A bit unclear what are you trying to do. So you want to create a variable with custom name at run-time and assign it a value? Maybe better solution would be to use an array and push elements trough the loop. You can access them with index: var n = 5; for (var i = 0; i < n; i += 1) { myarray.push(myvalue); } console.log(myarray[i]); // prints the value at index i. snowbillr 1 Link to comment Share on other sites More sharing options...
espace Posted April 10, 2017 Author Share Posted April 10, 2017 Hi, in fact i must store three parameter together to make a single Id. The level number The name of my object The number of my object in the array What is your advice about that ? Link to comment Share on other sites More sharing options...
snowbillr Posted April 10, 2017 Share Posted April 10, 2017 The deal is that you can't dynamically create a variable's name. Spinnerbox's suggestion of using an array makes sense since you are trying to keep a list of things. That's the perfect use case for an array Link to comment Share on other sites More sharing options...
mattstyles Posted April 10, 2017 Share Posted April 10, 2017 It doesn't really make any sense what you're trying to do, hence why JS (and most other languages) don't let you create dynamic variables, although you can normally create dynamic keys for an object. There are multiple reasons why but you'd probably be most concerned with referencing a dynamic variable later. Can't really say whether its the best representation for your data without knowing more about your use-case but one way to store your 3 things is just extending outwards from an array by keeping an array of objects, so long as all your object name keys are unique you'll have no issue, you'd end up with a structure like: var data = [ { foo: 21, bar: 22 }, { quux: 5, baz: 10 } ] Each entry in the array references your level number, with each object containing you name:number key:value pair. So, for example, to get the number of object `foo` from your first level you simply use: var number = data[0].foo // or const level = 0 const name = 'foo' var number = data[level][name] If this looks like a 2d array to you, well, its exactly the same due to the way JS stores objects (see associative arrays or hashmaps). If data structuring ever gets exciting then its probably working out the best representation of data that will achieve your needs and do this in the most performant way possible for your most important use-case/s. Link to comment Share on other sites More sharing options...
espace Posted April 10, 2017 Author Share Posted April 10, 2017 hi, i have found ! sorry to Mattstyles and Breed because i have not yet read their messages. but this work for me : window['level'+ i +'canon'+1]=450 alert(level0canon1) i read your messages tomorow it's time for dodo Link to comment Share on other sites More sharing options...
espace Posted April 11, 2017 Author Share Posted April 11, 2017 hi, Mattstyle and Breed. your solution is better than me and i was on the bad way. but how do you do to achieve this : var data = [ { canon0:"canon" , speed: 22 }, { canon1:"canon" , speed: 45 } //etc... ] with a loop ? ------------------------------------------------------------------------------------------------ the better for me would be this structure but i don't know if it's possible ? data[0].canon0="something" data[0].canon0.speed=45 data[0].canon1="something_else" data[0].canon1.speed=100 //// data[2].canon12="something_else" data[2].canon12.speed=200 Link to comment Share on other sites More sharing options...
espace Posted April 11, 2017 Author Share Posted April 11, 2017 ok i have learn something var t=[] t.level0=[] //t.level1 ...etc t.level0.push(["canon0",100,200]) //so to access the properties of canon0 (x and y) in level 0 i do: var canon.x=t.level0[0][1] var canon.y=t.level0[0][2] Link to comment Share on other sites More sharing options...
mattstyles Posted April 13, 2017 Share Posted April 13, 2017 Yep, no problem with representing your data as monads and stuffing them in to an array. Just as an aside, you can use computed object properties in all modern browsers (compat, you might have to flick the down arrow on syntax->object literal extensions to see it), e.g. (this is a super terse example): const key = 'foo' const data = [] for (let i = 0; i < 10; i++) { data.push({ [key + i]: 'bar' }) } I still think arrays are better than computed keys in your case though. Link to comment Share on other sites More sharing options...
spinnerbox Posted April 16, 2017 Share Posted April 16, 2017 On 10.4.2017 at 11:48 PM, espace3d said: hi, i have found ! sorry to Mattstyles and Breed because i have not yet read their messages. but this work for me : window['level'+ i +'canon'+1]=450 alert(level0canon1) i read your messages tomorow it's time for dodo Don't use the "window" keyword, its reserved for the DOM of your web site/application. Name it somehing like "gameWindow". On 13.4.2017 at 9:36 AM, mattstyles said: Yep, no problem with representing your data as monads and stuffing them in to an array. Just as an aside, you can use computed object properties in all modern browsers (compat, you might have to flick the down arrow on syntax->object literal extensions to see it), e.g. (this is a super terse example): const key = 'foo' const data = [] for (let i = 0; i < 10; i++) { data.push({ [key + i]: 'bar' }) } I still think arrays are better than computed keys in your case though. I also don't think dynamically generating keys in JavaScript object literal will work. The object does not exist yet at the time when you create your key. Unless this is ES6? Can you do this in ES6? Link to comment Share on other sites More sharing options...
snowbillr Posted April 16, 2017 Share Posted April 16, 2017 Yeah that's an ES6 thing, so you'll need to use a transpiler for that Link to comment Share on other sites More sharing options...
mattstyles Posted April 16, 2017 Share Posted April 16, 2017 4 hours ago, breed said: so you'll need to use a transpiler for that Nope, all modern browsers support it, see the compat table I linked earlier, its under computed properties. You only need to use a transpiler for it for legacy browser support. 5 hours ago, spinnerbox said: Unless this is ES6? Can you do this in ES6? Yep, and yep, and its in all modern browsers! Link to comment Share on other sites More sharing options...
snowbillr Posted April 16, 2017 Share Posted April 16, 2017 Oh wow it is! Geez I didn't realize all the modern ones were so up-to-date with ES6 features. Yeah so if you are good with only supporting modern browsers, go for it. If there's older browsers you want to support, you'll need to do a little extra. mattstyles 1 Link to comment Share on other sites More sharing options...
Recommended Posts