-1

Sorry I'm new here and my code will probably not be properly displayed...

how would I iterate through document to find arrays with .each()?

I was thinking something like this:

//-----START OF CODE----\\
var a = new Array("a0", "a1", "a3");
var b = new Array("b0", "b1", "b3");
var i = 0;

function CountArrays()
{    
    $('Array').each(function(){

        i++;            
    });
    alert("There are " + i + " arrays in this document!");
}

CountArrays();
//-----END OF CODE----\\

But $('Array') doesn't seem like the way to go, because I don't receive an alert!

Thanks for your help! :)

user1420563
  • 195
  • 1
  • 12

1 Answers1

1

No, no. This does not work. Please get familiar with the jQuery Library.

$('foo');

The $() function excepts a CSS Selector as a Parameter and returns an jQuery object, you can't put just variables into it. Your Javascripts and your HTML are two different things.

If you want to iterate over arrays in your code use the following:

var a = ["Hello","World"];
for (var i = 0; i < a.length; i++) {
    alert(a[i]);
}

There are plenty of ways to iterate over an array, see this for a reference.

Second: As mentioned using alert() to debug is not a good idea, especially when looping something. Use console.log('string', variable) instead when you are using Chrome or Firefox. It's writing your variables straight into the console and it also logs complete objects which is not possible with alert().

Update after comment: It's not possible to iterate over all existing arrays in your javascript. That's because you can't list all existing variables. One problem is, that your creating a new scope with every new function.

The variables inside the function don't know about the ones outside. So for example this is a valid piece of code:

var a = 5;

function foo() {
    var a = 6;

    return a;
}

console.log(a); // prints "5"
console.log(foo()); // prints "6"

Both variables are called a, but they contain different data. For further info see this answer: https://stackoverflow.com/a/2051693/735226

And just by the way: Why do you even want to know how many arrays there are in your scripts? And since objects can contain arrays like this

var objWithArray = {
    foo:5,
    bar:[3, 4, 5, 6, 7] // objWithArray.bar would be an array
};

there is no way to get all arrays. Javascript is a typeless and mutable language.

Community
  • 1
  • 1
Johannes Klauß
  • 10,676
  • 16
  • 68
  • 122
  • Thanks for your answer! Indeed I'm new to jQuery and I am trying to learn the Library, but what I want to do is not to iterate through the array! I am trying to know how many Arrays there are in my document :p How can I do that? – user1420563 May 27 '12 at 20:38
  • I didn't necessarily want to know how many I had, I just wanted to do something in the .each(), the alert was just to know if it worked! btw in my example it's supposed to return 2, because there are 2 arrays (a & b). So it's impossible? :( – user1420563 May 27 '12 at 20:58
  • As said in the answer, yes it's impossible. – Johannes Klauß May 27 '12 at 21:00
  • alright thanks a lot! I'll try to make it work with the example you gave :p – user1420563 May 27 '12 at 21:03