0

hi what can I use inspite of eval function for that code :

product_pcs          = 12;
product_code         = "lkb_12" ;
eval(product_code +"_pcs_1         = product_pcs");
alert (lkb_12_pcs_1);
momalone
  • 22
  • 4
  • 3
    Why do you need to assign to a variable with a dynamic name in the first place? Do you have multiple such variables? – Bergi Dec 21 '21 at 00:24
  • Not making extremely poor design decisions in your software I'd say is the #1 alternative. – connexo Dec 21 '21 at 00:26
  • I am getting datas from mysql with ajax and after success I have to change some which is "0" before ajax... in a loop.... – momalone Dec 21 '21 at 00:27
  • To remap property names, work with the objects you get from the DB and rename them in map calls. – connexo Dec 21 '21 at 00:30
  • @momalone In a loop? You mean like over an array? – Bergi Dec 21 '21 at 00:31

1 Answers1

0

I recommend you build an object instead

const products = {};

prodcuts['lkb_12'].pcs = 12;
prodcuts['lkb_12'].name = 'test';


prodcuts['lkb_13'].pcs = 14;
prodcuts['lkb_13'].name = 'test2';

// ...
``
Vincent Menzel
  • 1,040
  • 1
  • 6
  • 17