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 8342909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:54:08+00:00 2026-06-09T05:54:08+00:00

I am trying to set up an array in jQuery and I then need

  • 0

I am trying to set up an array in jQuery and I then need to do a for loop on it. But it seems that I cant use an associative array for some reason?

var items = new Array();
items['foo'] = 123456;
items['bar'] = 789012;
items['baz'] = 345678;
items['bat'] = 901234;
alert(items.length);

This is just a test, but it return 0?

  • 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-09T05:54:10+00:00Added an answer on June 9, 2026 at 5:54 am

    You can’t make associative array in JavaScript like what you want, instead you can use Object.

    For example:

    var items = {
      foo : 123456,
      bar : 789012,
      baz : 345678,
      bat : 901234
    }
    

    And to calculate the length you can do:

    var getObjectSize = function(obj) {
        var len = 0, key;
        for (key in obj) {
            if (obj.hasOwnProperty(key)) len++;
        }
        return len;
    };
    

    Use: getObjectSize(items); // output: 4

    For more see here.

    Another one is:

    Object.keys(items).length;
    

    But not supported by all browsers.

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

Sidebar

Related Questions

I'm trying to set up an array through jQuery's .each() function, but it seems
I'm trying to use an array to set the where parameters for a Zend
I am trying to set an array's slots using a for loop to be
I'm trying to merge two arrays for the values of array1 that are set
Trying to set it so if a certain condition is met then one of
I'm a little confused about which jQuery method and/or selectors to use when trying
I am trying to set the array keys as a strings like in the
I am trying to set the date format of the date picker element, but
Im sure its something simple, but i cant seem to set variable data from
I'm trying to use Google Maps API and i'm stuck here: I need an

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.