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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:46:42+00:00 2026-06-01T09:46:42+00:00

We have been using javascript hashes a lot lately, and we’ve been looking for

  • 0

We have been using javascript “hashes” a lot lately, and we’ve been looking for a universal way to count the items contained in both arrays and hashes without having to “know” which we’re dealing with except in the count method. As everyone knows .length is useless since it only returns the value of the highest index in the array. What we have below does not work because hashes test true for Array, but the length value returned is crap for hashes. We originally replaced .length all over our project with Object.keys().length, but this isn’t supported in IE8 and lower.

This is such a stupid simple thing and we can’t seem to get it working. Help me, Obi Wan. You’re my only hope!

function isNullOrUndefined(aObject) {
    "use strict";
    return (typeof aObject === 'undefined' || aObject === null);
}

function count(aList) {
    "use strict";
    var lKey = null,
        lResult = 0;
    if (!isNullOrUndefined(aList)) {
        if (aList.constructor == Array) {
            lResult = aList.length;
        } else if (!isNullOrUndefined(Object.keys)) {
            lResult = Object.keys(aList).length;
        } else {
            for (lKey in aList) {
                if (aList.hasOwnProperty(lKey)) {
                    lResult++;
                }
            }
        }
    }
    return lResult;
}
  • 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-01T09:46:43+00:00Added an answer on June 1, 2026 at 9:46 am

    Despise the answer from Raynos that is completely valid please consider performance

    This is how my hash object look like

    function Hash(){
       this.values = [];
       this.keys = {};
    }
    Hash.prototype.set = function(key, val){ 
       if(this.keys[key]){
          this.values[this.keys[key]] = value
       }else{
          this.keys[key] = (this.values.push(val)-1)
       }
    }
    Hash.prototype.length = function(){
        return this.values.length
    }
    

    Why I do this is simple performance looping through an object to count the properties length will be really inefficient the solution above give you direct access all the time.

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

Sidebar

Related Questions

I'm currently redesigning my website and have been looking into using JavaScript and jQuery.
I have been using JavaScript as a tool for many years now, but only
I have been using PHP and JavaScript for building my dad's website. He wants
I've been using javascript for a while, but have never learned the language past
Can anyone help? I have been designing a site using Javascript but the rest
I have been programming using PHP, Javascript and have been a designer for a
Hi I have been trying to learn Javascript using codeacademy.com and I have reached
I've been using various javascript frameworks including mootools, jquery, prototype and scriptaculous and have
I've been using Firebug to debug some javascript I have on one of my
I have been experimenting with using javascript to dynamically write stylesheets. In Firefox, one

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.