1

I have an array. Suppose the array is:

var group10 = ['as', '323fsd', 'asdasd', '43ssdf'];

I am passing the array into a function along with an array element so as to remove it from the array. If it removes, it will return true, otherwise, not.

function removeElem(element, array){
    var index = array.indexOf(element);
    if (index > -1) {
        array.splice(index, 1);
        return true;
    }
}

The tricky part is, I'm getting numeric variables dynamically from another function and it's returning either 1 or 2 or .......... 10

If it returns 1, then I want to deal with group1. If it returns 2, I want to deal with group2.

Since the name group is constant, I'm concatenating it with the variable it's returning and passing the whole into the function. But, unfortunately, a string is getting passed.

var arr = 'group'+10;

if( removeElem('323fsd', arr)) {
    console.log(group10);
} else {
    console.log('fail');
}

So, how is it possible to dynamically concatenating and passing the array as a whole into the function?

Vikrant
  • 4,920
  • 17
  • 48
  • 72
marukobotto
  • 759
  • 3
  • 12
  • 26

3 Answers3

2

eval() is a possible solution so the variable will be treated as a variable if exists in the same name

var group10 = ['as', '323fsd', 'asdasd', '43ssdf'];
var arr = 'group' + 10;

if(removeElem('323fsd', eval(arr))) {
    console.log(group10);
} else {
    console.log('fail');
}
Vikrant
  • 4,920
  • 17
  • 48
  • 72
Ryad Boubaker
  • 1,491
  • 11
  • 16
  • i suggest you to don't use eval it's like evil. that will make your code hell – Mahi Nov 24 '16 at 11:22
  • No sir that's why js has eval it's for such occasions and the most important part is that the eval is not getting it's param from a user input.. there is another alternative which is Using window[varname] but it has the side-effect of introducing global variables, which might not be wanted. – Ryad Boubaker Nov 24 '16 at 11:28
  • http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – Mahi Nov 24 '16 at 11:31
0

Try this:

Declare the array like this array = window[array];.Because the string not allowed directly converted into function or array.

var group10 = ['as', '323fsd', 'asdasd', '43ssdf'];
function removeElem(element, array){
  array = window[array];
 var index = array.indexOf(element);
 
  if (index > -1) {
                    array.splice(index, 1);
                    return true;
                }
 }

 var arr = 'group'+10;
 if( removeElem('323fsd', arr) ){
        console.log(group10);
    }else{
        console.log('fail');
    }
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

A proposal for using an array with the variables' name (assuming all variables are declared in advance).

function removeElem(element, arrayNo) {
    var groups = [group1, group2, group3, group4, group5, group6, group7, group8, group9, group10],
        index = groups[arrayNo - 1)].indexOf(element);

    if (index > -1) {
        groups[arrayNo - 1)].splice(index, 1);
        return true;
    }
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392