2

I have an array group with values let's say ab1,ab2,ab3...can be more. I would like to create each value an array and insert something into them as in array to use further. Somehow it's not working or maybe am doing something wrong. Please help.

array --> var group = new Array();
//This array has values ab1,ab2,ab3 as I see in alert() box

for(var a = 0; a < group.length; a++){
        //alert(group[a]); //individual group array gives output

        //creating array with the group name array value 
        //var justVar = group[a];
        //justVar = new Array();
        //justVar.push("yo!); 
        //alert(justVar);

        //creating array with the group name array value 
        var group[a] = new Array();
        group[a].push("Yo!");
        alert(group[a]); //No output

    }
4
  • could you also add to the question, the array that you would like to have at the end of execution Commented Jan 6, 2017 at 6:49
  • 2
    group should already be declared when you enter the loop. There is no need for the var keyword when setting the value of group[a]. Commented Jan 6, 2017 at 6:51
  • var group[a] is invalid syntax. Commented Jan 6, 2017 at 6:54
  • Okay, so can't var group[a] (which is suppose ab1) value be printed here and it can't be used as a new array() for further. Commented Jan 6, 2017 at 6:57

4 Answers 4

2

var group[a] is invalid syntax in javascript. Other than that, the loop looks good.

var group = new Array("ab1","ab2","ab3");
//This array has values ab1,ab2,ab3 as I see in alert() box

for(var a = 0; a < group.length; a++){
        //alert(group[a]); //individual group array gives output

        //creating array with the group name array value 
        //var justVar = group[a];
        //justVar = new Array();
        //justVar.push("yo!); 
        //alert(justVar);

        //creating array with the group name array value 
        group[a] = new Array();
        group[a].push("Yo!");
        console.log(group[a]); //No output

    }

If you want "ab1" to become an array you can do it like group[a] = [group[a]];

var group = new Array("ab1","ab2","ab3");
//This array has values ab1,ab2,ab3 as I see in alert() box

for(var a = 0; a < group.length; a++){
        //alert(group[a]); //individual group array gives output

        //creating array with the group name array value 
        //var justVar = group[a];
        //justVar = new Array();
        //justVar.push("yo!); 
        //alert(justVar);

        //creating array with the group name array value 
        group[a] = [group[a]];
        console.log(group[a]); //No output

    }

Sign up to request clarification or add additional context in comments.

3 Comments

yes, you are correct. Thank you, the code seems to work now. the only part causing issues is var group[a] which is an invalid syntax as you say. But it works fine while I created group array. var group = new Array(); Here, it's fine?
var group[x] is wrong syntax as you cannot have [] in variable names. [] is used to refer to array indices. Javascript tried to look at group[x] as a variable name when used with var.
great, thank you so much all here and you for quick answer. I will keep that in my little brain from next time. Thank you again.
1

In your code, you are re-initializing array again and gain in the loop.

var group = ['ab1','ab2','ab3'];

for(var a = 0; a < group.length; a++){
        

  //creating array with the group name array value 
  group[a] = new Array();
  group[a].push("Yo!");
  alert(group[a]); //No output

}

console.log(group)

1 Comment

using var in loop doesn't reinitialise values again and again. Variable declarations will be hoisted in javascript. The syntax is wrong here though!
1

Seems like you want to create variable using array value, you can use following code for such things :

var group = ['a1', 'a2'];
var obj = {};

for (var a = 0; a < group.length; a++) {

  obj[group[a]] = new Array()
  obj[group[a]].push("Yo!");

}

console.log(obj);
console.log(obj.a1);
console.log(obj.a2);

Comments

0

you have to declare the array outside your for variable.

var group= ['a','b','c'];
for (var a =1; a<group.length; a++)
{
   group[a] = new Array();
    group[a].push("Yo!");
    group[a].push("check this!");
    group[a].push("out!");    
}
 console.log(group);

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.