1

Here is code that I have copied from w3schools, I have different code, but the problem that I am having still happens in this simplified coding.

<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to create an array, then display it's length</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var foods = ["steak","pizza","bread","fruits"];
var x=document.getElementById("demo");
var y=foods[3];
x.innerHTML= y.length;
}
</script>

</body>
</html>

When I do x.innerHTML= fruits.length;, I get 4 back which is what I want to get.

But when I call

var y=foods[3];
x.innerHTML= y.length;

I get 6 which is the length of the word "fruits" but I want the length of the array fruits.

How do I do this?

I'm using jQuery, don't know if that affects anything. Do I have to add parenthesis or brackets somewhere?

9

5 Answers 5

3

If (and only if) a variable (myVar) contains the name of a property of another object (myObj) you can use:

myObj[myVar]

to access that property.

In this case, your fruits array is just a normal local variable, so there's no way (short of the frowned-upon eval function) to access it indirectly.

You can of course use fruits.length to directly find its length.

A better solution would be a nested object of foods, and not an array:

var foods = {
    fruits: [ "Banana", "Orange", "Apple", "Mango" ],
    steak: [ ... ],
    pizza: [ ... ],
    bread: [ ... ]
};

at which point you can use the syntax above and write:

var myType = 'fruits';
var count = foods[myType].length;
Sign up to request clarification or add additional context in comments.

Comments

2

Maybe what you want is

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var foods = ["steak","pizza","bread", fruits];

This way your code will work, since we have stored a reference to the array fruits inside the foods instead of the string "fruits".

2 Comments

this will work, but IMHO it's not a good solution. I'm pretty sure the OP wants to use the string "fruits" to indirect into that array.
@Alnitak, i am sure using an object is the proper solution to such problems. My answer is a bit directly based on the var y = foods[3]; part of the question..
2

You seem to have a huge misunderstanding of the difference between the variable identifier fruits and the string "fruits".

Let's describe your code together so you understand well what you are doing.


  • var fruits = ["Banana", "Orange", "Apple", "Mango"];

You have a variable named fruits which identifies an array of four elements, all strings. You could do the following with that array:

alert(fruits.length); // 4
alert(fruits[0]); // Banana
alert(fruits[3]); // Mango
alert(fruits); // Banana, Orange, Apple, Mango

  • var foods = ["steak","pizza","bread","fruits"];

You have a variable named foods which identifies an array of four elements, again: all strings. You could again do the following:

alert(foods.length); // 4
alert(foods[0]); // steak
alert(foods[3]); // fruits
alert(foods); // steak, pizza, bread, fruits

  • You try to count the fruits, doing foods[3].length

You should notice that when we did alert(fruits), it displayed Banana, Orange, Apple, Mango. And when we did alert(foods[3]), it displayed fruits. This should give you a hint: the array you named fruits and the string "fruits" are two different things!

That is why foods[3].length is 6, because the length of "fruits" is 6.


Solution

EDIT: Check Altiniak's answer. I leave the rest of my post for the long and valid explaination. The idea of using window was almost worse than using eval()...

8 Comments

fruits is not a global variable so window[foods[3]].length; will not work. The OP's code is inside a function so nothing get store in the global scope..
My mistake. I will edit, but there is nothing to add to Alnitak's answer anymore ;) I would upvote his if I could :P
@snooze92 downvote removed - you're neutral now ;-) Why can't you upvote?
@Alnitak I created my StackOverflow account 2h ago, still missing 6 "reputation points" until I can upvote :)
@snooze92 oh yeah, I forgot that new accounts can't even upvote :)
|
1

In the following:

var y=foods[3];
x.innerHTML= y.length;"

You are assigning the 4th element in the foods array to the y variable, which is the string "fruits". If you want the length of the array, just do this:

x.innerHTML = foods.length;

EDIT: I am assuming you just want the length of the foods array and NOT to store the fruits array in the foods variable (if you need that, Gaby's answer can give you that)

7 Comments

He actually wants to access the fruits array by using the foods array.
@Amberlamps OP says "I get 6 which is the length of the word "fruit" but i want the length of the array fruit."
@mattytommo: Exactly. foods[3] is the word fruits and he wants to access the variable with that name. I had to read it a couple of times until I understood what the OP wants.
@mattytommo: I am pretty sure, that he wants what I described and also what Felix said.
The OP clearly wants to indirect from the string "fruits" into the local array of the same name.
|
0

Hope I get what you want. You you have the word "fruits" and from that, you want to fetch the contents of the var named fruits, right?

That can be done using eval.

var fruitlen = eval('fruits').length;

Is this what you wanted?

1 Comment

@ChristopheL Then let's hope this is not what the OP is asking for! Oh, and I like Alnitak's answer.

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.