0

I got an 2x10 array and I need set a variable to any member of that array. Make it by hands its not cool, so Im trying to declarate by for operator:

    allImages=[
    [
        'img1-1','img1-2', 'img1-3', 'img1-4', 'img1-5'
    ],[
        'img2-1','img2-2', 'img2-3', 'img2-4', 'img2-5'
    ]
];

for(i=0;i<1;i++){
    console.log(i + ' part ------------------------');
    for(j=0;j<5;j++){

        x+(i+'-'+j) = allImages[i][j];
        console.log((x+(i+'-'+j)) + '-> item');
    }
}

But looks like I make a primitive error:

Invalid left-hand side in assignment

Anyway, I cant figure out how to solve this. Can anyone say how to declarate a lot of variables with custom keys throw for operator or with another method?

----- My solution by(https://stackoverflow.com/users/1230836/elias-van-ootegem):

var statImg = {};
var blurImg ={};

for (var i = 0; i < 13; i++) {
    var keyName = 'img'+i;
    var valOfKey = 'img/'+i+'.png'
    statImg[keyName] = valOfKey;
    blurImg[keyName] = valOfKey;
};
Community
  • 1
  • 1
weird-dreams
  • 159
  • 2
  • 16
  • 1
    You don't. Arrays and objects are used to hold multiple related values. Why do you think you *need* a variable for each value? – Felix Kling Jun 25 '13 at 10:00
  • possible duplicate of [Access Javascript variables dynamically](http://stackoverflow.com/questions/11515924/access-javascript-variables-dynamically) – Felix Kling Jun 25 '13 at 10:05
  • not for me, but thanks – weird-dreams Jun 25 '13 at 10:39
  • Well, even in your solution, there is no need for an object. All your key names are of the form `img{Index}`. You could just use an array instead: `statImg[i] = valOfKey;`. In any case, there are many, many duplicates for this question: http://stackoverflow.com/search?q=[javascript]+dynamic+variable+name. – Felix Kling Jun 25 '13 at 10:45

1 Answers1

1

You'll have to either create an object, and use the left-hand trickery you're trying to generate property names, or you'll have to fall back to the global object (which I hope you don't):

var names = {};//create object
//-> in loop:
names[ x+(i+'-'+j)] = allImages[i][j];

To be complete, but again: don't actually go and do this, you could replace names with window. In which case, you'll be polluting the global scope.
Perhaps you might want to check the values (like x, i and j) for values that make it "difficult" to access the properties, like %, or indeed the dash you're concatenating in your example:

var anObj = {};
anObj['my-property'] = 'this is valid';
console.log(anObj.my-property);//ReferenceError: property is not defined

That is because the dash, or decrement operator isn't seen as part of the property. Eitherway, using separate variables is, in your case, not the best way to go. Programming languages support arrays and objects because of this very reason: grouping related data, making them easy to access through a single variable.
If needs must, just use an object, if not, construct an array you sort using array.sort(function(){});
check MDN on how to acchieve this, if you're stuck down the way, let us know....

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149