Jump to content

concatenate and loop need help


espace
 Share

Recommended Posts

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

	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

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. 

 

Link to comment
Share on other sites

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

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

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

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? :D

Link to comment
Share on other sites

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...