1

Hi I want to get alert "lkb_1" and "lkb_2" , but this code gives me "product_1_name" and "product_1_name" ;

eval and window functions I can get rid of them but I think they are not recommended to do that.. so is there any other solution rather than eval and window functions?

<script>
  product_1_name = "lkb_1";
  product_1_pcs = 3;
  product_2_name = "lkb_2";
  product_2_pcs = 5;
  profil_ismi = "";

  const profil_listesi = ["product_1", "product_2"];
  i = 0;
  while (i < profil_listesi.length) {
    profil_ismi = profil_listesi[i] + "_name";
    alert(profil_ismi);
    i = i + 1;
  }

</script>
Joshua
  • 3,055
  • 3
  • 22
  • 37
  • 2
    You are adding `"_name"` manually inside the `whie` loop with `profil_ismi = profil_listesi[i] + "_name";`. Avoid that and your code will work as expected – Nitheesh Jan 07 '22 at 08:31
  • Remove `+ "_name";`. Also take a look at this answer : [Loop through an array in JavaScript](https://stackoverflow.com/a/3010848/16688813) – Tom Jan 07 '22 at 08:31
  • I understand I asked question wrong.. let me update it – Haluk Aydoğan Jan 07 '22 at 08:42
  • Does this answer your question? https://stackoverflow.com/a/1664294/1727948 – Joshua Jan 07 '22 at 08:52
  • I know eval and windows methods.. but I dont know why, in forums they are not recomended.. this is why I ask what is those 2 methods altarnatives? – Haluk Aydoğan Jan 07 '22 at 08:56

2 Answers2

1

To avoid using eval you can do that.

The thing would work well if you wrap the product names in an array. then you can dynamically create the variable name and thus specifically retrieve the value in the array.

arr = []
arr['product_1_name'] = "lkb_1" ;
product_1_pcs       = 3 ;
    
arr['product_2_name'] = "lkb_2" ;
product_2_pcs       = 5 ;
                    
profil_ismi = "" ;
    
const profil_listesi = ["product_1","product_2"];
let i=0;
while (i<profil_listesi.length)
{
  let _name = profil_listesi[i] + '_name'
   profil_ismi     = arr[_name];
       
   alert (profil_ismi) ;
    
   i=i+1;
}
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • this is better or windows method is better ? 'profil_ismi = window[profil_listesi[i] + "_name"];' – Haluk Aydoğan Jan 07 '22 at 08:55
  • is the same. but why you want put this in the window object. – Maik Lowrey Jan 07 '22 at 08:56
  • 1
    ok so I will use your method. thanks for replay.. by the way.. can you just give me a little brief why eval is evil? – Haluk Aydoğan Jan 07 '22 at 08:58
  • I mean cause , safety ? or performances? – Haluk Aydoğan Jan 07 '22 at 09:02
  • @HalukAydoğan That is good question :-) Perfomance is minimal and can be neglected. Rather security. But here you have to distinguish whether it is server-side or client-side. If it's server-side, eval is evil. If it's client-side, it doesn't really matter. – Maik Lowrey Jan 07 '22 at 09:07
  • 1
    thanks for informations.. now I get a clue to understand the full of puzzle... by the way those profil_names will go throught to ajax and php and myql ... so eval is evil for me... – Haluk Aydoğan Jan 07 '22 at 09:16
1

You can use eval in this case. It is safe, because it is evident that it cannot be misused for code injection in this particular case.

Still, I think it would be better (more scalable, more future proof), to have an array of products, as follows:

var products = [
  { id: 'product_1', name: 'lkb_1', pcs: '3'},
  { id: 'product_2', name: 'lkb_2', pcs: '5'}
];

const profil_listesi = ["product_1", "product_2"];
i = 0;
while (i < profil_listesi.length) {
  profil_ismi = products.find(p => p.id === profil_listesi[i])
  alert(profil_ismi.name);
  i = i + 1;
}
www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32