1

so imagine I have an array like this:

var array = ['Apple','Orange','Banana','Melon'];

And I want to create variables just by looking at that array, like following:

var apple;
var orange;
var banana;
var melon;

How can I achieve this without using objects?

Mustafa Bereket
  • 129
  • 2
  • 13

2 Answers2

1

In the global scope you could create the variables using window object

var array = ['Apple','Orange','Banana','Melon'];

array.forEach(e => window[e.toLowerCase()] = e);

console.log(apple); // Apple
isvforall
  • 8,768
  • 6
  • 35
  • 50
1

You can always do this:

    var array = ['Apple','Orange','Banana','Melon'];
    for(var i = 0 ; i < array.length ; i++) 
        this[array[i].toLowerCase()] = 'something...';

If you are in general scope this is window otherwise it is the local context

Hirad Nikoo
  • 1,599
  • 16
  • 26