2

So I have some arrays (in reality I have about 30-40):

var p1  = ["John", "bio", "john.png"];
var p2  = ["Kate", "bio", "kate.png"];
var p3  = ["Mary", "bio", "mary.png"];

which have the respective information for each person I want to use in my HTML.

I want to add each of these arrays to another array to have a final result of:

var people = [["John", "bio", "john.png"], 
              ["Kate", "bio", "kate.png"], 
              ["Mary", "bio", "mary.png"]];

Is there a way of adding all these p1 , p2, p3 arrays to the people array using a for loop?

I tried this:

for (var i = 1; i <= 30; i++) {
    var toPush = "p" + i;
    people.push(toPush);
}

But obviously this just creates and pushes strings into the array. How can I get around this?

Thanks!

ybp
  • 21
  • 1
  • 1
    Why are you not adding initializing them in a `people` array in the first place? `var people = [["John", "bio", "john.jpg"], ["Kate", "bio", "kate.jpg"], etc];` – Brennan Jun 04 '15 at 17:10
  • 2
    Just use `people = [p1, p2, p3]`. If you don't like to type out thirty different variable names, don't use 30 variables in the first place. – Bergi Jun 04 '15 at 17:14

3 Answers3

3

You should start with a people array instead of creating individual variables for each array create your arrays like this:

var people = new Array();
people.push(["John", "bio", "john.png"]);
people.push(["Kate", "bio", "kate.png"]);

etc...

brso05
  • 13,142
  • 2
  • 21
  • 40
0

You can use javascript eval for this purpose but it's not recommended. For issues with eval() please see https://stackoverflow.com/a/86580/4810628

function l(){
  var p1 = ["John", "bio", "john.png"];
  var p2 = ["Kate", "bio", "kate.png"];
  var p3 = ["Mary", "bio", "mary.png"];



  var people = [
    ["John", "bio", "john.png"],
    ["Kate", "bio", "kate.png"],
    ["Mary", "bio", "mary.png"]
  ];
  alert("length before adding elements:"+people.length);

  for (i = 1; i <= 3; i++)

  {
    people.push(eval("p" + i));
  }

  alert("length after elements added:"+people.length);

}
<body onload="return l()">
  
  </body>
Community
  • 1
  • 1
user786
  • 3,902
  • 4
  • 40
  • 72
  • Why is there jQuery code in this answer? No `jquery` tag is present on the question, it could be confusing to the poster. You're also using a dangerous `eval` here... – Cᴏʀʏ Jun 04 '15 at 17:28
  • @Cᴏʀʏ thanks for the info. here it says it's open to injection http://stackoverflow.com/a/86580/4810628 can you please add few words how an injection attack can happen with eval? – user786 Jun 04 '15 at 17:50
  • 1
    Honestly, for this trivial example, there's technically no danger, but as using it at all is generally frowned upon, it's better to opt for code that excludes it altogether. – Cᴏʀʏ Jun 04 '15 at 18:08
0
function ArrayList()
{
  var people=[];
  people.push(["John", "bio", "john.png"]);
  people.push(["Kate", "bio", "kate.png"]);
  people.push(["Mary", "bio", "mary.png"]);

    for(var j=0;j<people.length;j++)
    {
        console.log( people[j] );
        alert(people[j]);
    }
}

http://jsfiddle.net/ys9jptsn/

jignesh
  • 1,639
  • 1
  • 16
  • 18