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!

2
  • 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]; Commented Jun 4, 2015 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. Commented Jun 4, 2015 at 17:14

3 Answers 3

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...

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

2 Comments

Personally, I’d use var people = []; over var people = new Array();
You're right, I didn't realize how pointless what I was doing was until I read this.. :D Thanks!
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/

Comments

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>

3 Comments

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ᴏʀʏ thanks for the info. here it says it's open to injection stackoverflow.com/a/86580/4810628 can you please add few words how an injection attack can happen with eval?
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.

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.