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?

5
  • removeElem('323fsd', window[arr]) Commented Nov 24, 2016 at 11:03
  • 1
    try eval(arr) so it can be treated as a variable not a string Commented Nov 24, 2016 at 11:03
  • 1
    Possible duplicate of How to use a string as a variable name in Javascript? Commented Nov 24, 2016 at 11:04
  • please explain "I'm getting numeric variables dynamically from another function" and where the variables get declared and how they get initialized. Commented Nov 24, 2016 at 12:00
  • Any time you find yourself using numeric variables like this, you should be using an array instead. Commented Nov 24, 2016 at 12:21

3 Answers 3

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');
}
Sign up to request clarification or add additional context in comments.

3 Comments

i suggest you to don't use eval it's like evil. that will make your code hell
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.
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');
    }

Comments

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.