1
var thisdata = $.jStorage.get(something);
var nameChi = "nameChi" + i;
var name = "name" + i;
var brought = "brought" + i;
var amount = "amount" + i;
var np = "np" + i;
var num = "num" + i;

Above is working code which I want to simplify. I tried to turn individual assignments into a for loop with the code below:

thisdata = $.jStorage.get(something);
var generateid = ['thisdata', 'nameChi', 'name', 'brought', 'amount', 'np', 'num']

for (var a in generateid){
    var generateid[a] = generateid[a] + i
}

However, this is not working. Can anyone help me to solve this? Thanks a lot!!

10
  • You can't create dynamic variables like that. Just assign the values to another array Commented Dec 29, 2016 at 2:30
  • ^^ as said.. you can't create a dynamic variable but you can create a object with dynamic property. That is a close one to what you are trying Commented Dec 29, 2016 at 2:30
  • There's lots of things at play here - using a for-in to iterate over an array, using the var keyword when trying to mutate that array, and a possible misunderstanding of when to use Objects. Commented Dec 29, 2016 at 2:31
  • what do you do with the variables later on? Commented Dec 29, 2016 at 2:32
  • Thanks for you guys comments!! @JochenBedersdorfer , actually i am using a noob way to get data from a json file (let's say data A & data B), and I try to make those data (A1, A2, A3, B1, B2, B3) viewable (bring them to front end). Commented Dec 29, 2016 at 2:38

4 Answers 4

2

Try this

var generateid = ['thisdata', 'nameChi', 'name', 'brought', 'amount', 'np', 'num']
generateid = generateid.map(each => each + i);

How does this work?

  • map create a new array with the result of function
  • each => each + i is an anonymous function that adds i to each element
Sign up to request clarification or add additional context in comments.

4 Comments

Where does i come from?
Where ever it comes from in the example given in the question …
@akuhn Fair call :)
The OP wants to be able to generate variable names dynamically and not update his array values .. you can sense that intent of OP if you look at his code
0

You can use destructuring assignment, Array.prototype.map()

var generated = ["nameChi", "name", "brought", "amount", "np", "num"]
                .map((n /*,i*/) => n + i);
var [nameChi, name, brought, amount, np, num] = generated;

Comments

0

In your code the Index is the problem for the assignment, Use this following code for the usage,

var val;
var generateid = ['thisdata', 'nameChi', 'name', 'brought', 'amount', 'np', 'num'];
var index=0;
for (val of generateid ) {
    generateid [index]=val + "i"
}

How is works,

  1. In every for loop index will be incremented, So you can find the insertion index using that index value.

Comments

0

Try this it will work.

 var ids = ["thisdata", "nameChi", "name", "brought", "amount", "np", "num"];
 var generateid = {};
 for(var i = 0; i < ids.length; i++){ 
      generateid[ids[i]] = ids[i] + i; 
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.