Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8356581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:14:04+00:00 2026-06-09T10:14:04+00:00

I have an odd problem. I’m trying to use Javascript to fetch me some

  • 0

I have an odd problem. I’m trying to use Javascript to fetch me some values from a multidimensional array, and it’s giving me some weird output.

Here is my code:

foo = [['3','4'],['5','6']];

for (bar in foo) {

    baz = bar[0];
    alert(baz);

    qux = bar[1];
    alert(qux);

}

Here is the output of the above:

// These are all alerts, by the way
0,undefined,1,undefined,$,f,$,c,e,a,c,l,c,l,i,i,n,a,s,l,i,c,o,a,p,g,e,g,e,i,n,c,o,e,r,e,m,f,l,p,i,h,e,r,g       

Can somebody tell me what is happening?

Here is a jsFiddle of the problem: http://jsfiddle.net/Jey6w/

Edit:

Here is another jsFiddle, with another layer of “Inception”: http://jsfiddle.net/8vyGq/

The output:

// Again, these are all alerts, and * signifies undefined
0**1**$ff$ceaacllcllinnassliicooappgeegeeinncooerremmfllpiiheergg 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:14:05+00:00Added an answer on June 9, 2026 at 10:14 am

    I could be wrong, but I think it’s due to the fact that bar is returning a reference to a property within an object. Changing your selectors to foo[bar][0] works a treat.

    foo = [['3','4'],['5','6']];
    
    for (bar in foo) {
        alert(foo[bar][0]);
        alert(foo[bar][1]);
    }​
    

    In cases where your object is simply a multi-dimensional array, I would sway array from using the for in statement, as it can select unwanted properties. I would stick to the good old fashioned for(start, stop, increment)

    foo = [['3','4'],['5','6']];
    
    for (i = 0; i < foo.length; i++) {
        alert(foo[i][0]);
        alert(foo[i][1]);
    }​
    

    Update – jQuery

    As there has been mention of jQuery’s .each method I thought I’d also post an example of how it could be utilised. The jQuery’s each method passes 2 optional parameters, indexInArray, and valueOfElement. Additionally, the jQuery documentation also states that

    The value can also be accessed through the this keyword, but
    Javascript will always wrap the this value as an Object even if it is
    a simple string or number value

    With this in mind, we could achieve the same results as previous example, using the following jQuery (jsFiddle):

    var foo = [['3','4'],['5','6']];
    
    $.each(foo, function() {
        // 'this' is the context of the current item within the array
        alert(this[0]);
        alert(this[1]);
    }​)​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an odd problem with MVC Razor @ escaping. In some javascript in
I have an odd problem. I'm trying to echo the response from a file
I have an odd problem in silverlight. I use the following XAML to bind
So, I have an odd problem. I'm working with an SQLite database through javascript
I have a very odd problem with javascript. My code is rather long so
I have a very odd problem. Whenever I try to use the Add View
I have an odd problem. Our company collects data and we use a HORRIBLE
I seem to have an odd problem while trying to add objects to a
I have an odd problem. I need to export Japanese characters from a table
I have a very odd problem with the following code. For some reason I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.