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!!

Kevin
  • 33
  • 5
  • You can't create dynamic variables like that. Just assign the values to another array – Phil Dec 29 '16 at 02: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 – Rajshekar Reddy Dec 29 '16 at 02: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. – Jack Guy Dec 29 '16 at 02:31
  • what do you do with the variables later on? – Jochen Bedersdorfer Dec 29 '16 at 02: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). – Kevin Dec 29 '16 at 02:38
  • @YuenKevin Are you trying to create an array of objects? – guest271314 Dec 29 '16 at 02:44
  • @RobG OP is trying to assign variable identifiers to elements of an existing array. – guest271314 Dec 29 '16 at 02:48
  • @guest271314 kind of, the array is created already, I'm trying to take that array(s) and display them into my own html file. – Kevin Dec 29 '16 at 02:49
  • @YuenKevin Are you simultaneously trying to create a variable identifier for each of the elements of the array? – guest271314 Dec 29 '16 at 02:50
  • This is what you should consider doing : https://jsfiddle.net/nikdtu/swb5vrnr/ – Nikhil Maheshwari Dec 29 '16 at 02:51

4 Answers4

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
akuhn
  • 27,477
  • 2
  • 76
  • 91
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;
guest271314
  • 1
  • 15
  • 104
  • 177
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.
K.Suthagar
  • 2,226
  • 1
  • 16
  • 28
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; 
 }
Anil Talla
  • 709
  • 5
  • 19