Home > Net >  Is there a way to declare a variable by adding a variable name with a variable value?
Is there a way to declare a variable by adding a variable name with a variable value?

Time:07-10

I have some arrays:

var orderObj1 = [], orderObj2  = [], orderObj3 = [], orderObj4 = [], orderObj5 = [], orderObj6= [];

And I'm trying to do something like this:

for (let [key, value] of Object.entries(req.user.orders[z])) {
  if (key == orderName[a]   "Name") {
  a   1;
  var ordName = orderObj   a;
  ordName.push(value);
 }

The end goal is ordName = orderObj1, and as a increases, ordName will reference orderObj2, orderObj3 and so on. The value of a initially is 0.

CodePudding user response:

You can store them in an object

var orders = {}
for (let [key, value] of Object.entries(req.user.orders[z])) {
    if (key == orderName[a]   "Name") {
    a   1;
    var ordName = orders["orderObj"   a] = []
    ordName.push(value);
}

but native variables, I think you can only get them by using eval and I would not recommend that

  • Related