0

I am trying to pass an array value into a function as an arg and then use it's value to reference another array. I know that sounds a bit complicated but I am doing it for a reason ;) I THINK that showProject thinks that project1 is a string as it returns only 'p' and ... Any help appreciated?

var projects = ["project1","project2"];
var project1 = ["image1","image2"];
var project2 = ["image1","image2"];
function showProject(arr){
 console.log(arr[0]);
}
//why isn't this showing image 1 in project 1 array?
showProject(projects[0]);

Fiddle> http://jsfiddle.net/4ryLT/

asked Jan 13, 2014 at 9:58
3
  • smells like you need to use JSON :) Commented Jan 13, 2014 at 10:00
  • 1
    Yes, you are passing the string "project1" to the function which correclty alerts "p". So what is the question? Commented Jan 13, 2014 at 10:00
  • why isn't this showing image 1 in project 1 array? because project1 neither project2 are being used inside showProject(). Commented Jan 13, 2014 at 10:02

5 Answers 5

4

This seems to be what you want:

jsFiddle

var project1 = ["image1","image2"];
var project2 = ["image1","image2"];
var projects = [project1,project2];
function showProject(arr){
 alert(arr[0]);
}
showProject(projects[0]);

Set arrays as elements of projects array.

answered Jan 13, 2014 at 10:04
Sign up to request clarification or add additional context in comments.

1 Comment

My cookies, you took em! ='(
0

is var projects referring to the other two variables? If so, remove the "" and put them in a different order

var project1 = ["image1","image2"];
var project2 = ["image1","image2"];
var projects = [project1,project2];
function showProject(arr){
alert(arr[0]);
}
showProject(projects[0]);

http://jsfiddle.net/4ryLT/3/

answered Jan 13, 2014 at 10:03

Comments

0
var projects = ["project1","project2"];
var project1 = ["image1","image2"];
var project2 = ["image1","image2"];
function showProject(arr){
 console.log(eval(arr)[0]);
}
showProject(projects[0]);
Frogmouth
1,8161 gold badge14 silver badges22 bronze badges
answered Jan 13, 2014 at 10:02

Comments

0

I think you mean this

var projects = [];
projects["project1"] = ["image1","image2"];
projects["project2"] = ["image1","image2"];
function showProject(arr){
 alert(arr[0]);
}
showProject(projects["project1"]);
answered Jan 13, 2014 at 10:22

Comments

-1

without change your logic:

var projects = ["project1","project2"];
var project1 = ["image1","image2"];
var project2 = ["image1","image2"];
function showProject(arr){
 console.log(eval(arr)[0]);
}
showProject(projects[0]);

byee

answered Jan 13, 2014 at 10:12

2 Comments

How is this different than @AlexanderNenkov's answer?
i see the @AlexanderNenkov after post. Because before my post the AlexanderNenkov answer was "unformatted". if you see i edit the answer and than i see that we write same code... your comment is useless.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.