espace Posted October 29, 2017 Share Posted October 29, 2017 hi, i want to do this : l0.a=0 l1.a=1 so i do this : for (var i = 0; i < 2; i++){'l'+i={a:i}} but it say ReferenceError: invalid assignment left-hand side how do you that ? Quote Link to comment Share on other sites More sharing options...
bubamara Posted October 29, 2017 Share Posted October 29, 2017 nice way var l = []; for (var i=0; i < 2; i++) { l.push( { a : i } ); } console.log(l[0], l[1]); in this case, this === window -> better not pollute it, but you can use it on your own object without worries for (var i=0; i < 2; i++) { this[ 'l' + i ] = { a : i }; } console.log(this); console.log(this.l0, this.l1); nasty way for (var i=0; i < 2; i++) { eval( 'l' + i + ' = { a : i } ' ); } console.log(l0, l1); espace 1 Quote Link to comment Share on other sites More sharing options...
ugajin Posted October 29, 2017 Share Posted October 29, 2017 Do you mean this... for (var i=0; i < 2; i++) { console.log('1' + i.toString() + '.a=' + i.toString()); ; } Returns... 10.a=0 11.a=1 espace 1 Quote Link to comment Share on other sites More sharing options...
espace Posted October 30, 2017 Author Share Posted October 30, 2017 thank you so much bubamara with your example i make incredible things since, thanks. and thanks also to ugajin it works too. have a nice day. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.