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

  • SEARCH
  • Home
  • 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 8009577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:29:25+00:00 2026-06-04T18:29:25+00:00

I have an array with both integer and string index. For some reason, it

  • 0

I have an array with both integer and string index.

For some reason, it seems that $.each does not correctly iterate over the string indexes.

The below output is:

idx: 0
1
2
3

idx: 1
4
5
6

idx: 2
7
8
9

// actual
idx: 3
a
b
c

// expected
idx: abc
10
11
12

Here is the code I tested with:

<html>
<head>
    <title>jQuery - each</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {

            var a = new Array();
            a.push(0);
            a[0] = [1, 2, 3];

            a.push(1);
            a[1] = [4, 5, 6];

            a.push(2);
            a[2] = [7, 8, 9];

            a.push("abc");
            a["abc"] = [10, 11, 12];

            $.each(a, function (idx, v) {

                alert("idx: " + idx);
                alert(v[0]);
                alert(v[1]);
                alert(v[2]);
            });
        });

    </script>
</head>
<body>
</body>

Thank you very much for your help,

Richard Hughes

  • 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-04T18:29:27+00:00Added an answer on June 4, 2026 at 6:29 pm

    change your code like so:

     $(document).ready(function () {
    
            var a = {};
            a['0'] = [1, 2, 3];
            a['1'] = [4, 5, 6];
            a['2'] = [7, 8, 9];
            a['abc'] = [10, 11, 12];
    
            $.each(a, function (idx, v) {
                console.log("idx %s : %s, %s, %s", idx, v[0], v[1], v[2]);
            });
      });
    

    this will return

    idx 0 : 1, 2, 3
    idx 1 : 4, 5, 6
    idx 2 : 7, 8, 9
    idx abc : 10, 11, 12
    

    Note that when you use a string as index, your data structure is an object (and not an array)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In .NET, both array and list have Enumerable as ancestor, so a method that
I have both matrices containing only ones and each array has 500 rows and
I have a 2D array called results. Each row array in results contains both
Why does the following code compile and run. I have created an integer array
I have an array with keys and values. Each value is an integer. I
I have a randomly ordered C array of integer numbers. Each of these numbers
I have an array and an external file, both contains lots of words. I
I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit
Both ArrayController and CollectionView have same functionality to render 'content' array using template, except
I have many constant arrays that do not all have the same number of

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.