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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:46:13+00:00 2026-05-26T19:46:13+00:00

Why does my array length always come out to 0 even though var email

  • 0

Why does my array length always come out to 0 even though var email is equal to a string. (I’ve alerted out var email and the data is there).

    var emails = new Array();

    //get all the emails
    $('.emailBox input').each(function (i)
    {
        var email = $(this).val();

        if(email != '')
        {
            emails[email] = email;
            alert(emails.length);
        }
    });
  • 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-05-26T19:46:14+00:00Added an answer on May 26, 2026 at 7:46 pm

    Because you’re adding a property to the array.

    var a = [];
    a.foo = 42;
    a.length === 0; // true
    

    Instead try

    emails.push(email);

    This is the same as emails[emails.length] = email

    As an aside:

    var emails = new Array();

    Is bad. You should be using [] instead of new Array() mainly because it’s more terse and readable.

    if (email != '') {

    The above can be replace with if (email) { in case jQuery ever returns undefined or null

    To make the entire code more elegant you should use

    var emails = $('.emailBox input').map(function() {
        return this.value;
    }).filter(function (k, v) { return v; }).get();
    

    Or without jQuery

    var emails = [].map.call(document.querySelectorAll(".emailBox input"), function (v) {
        return v.value;
    }).filter(function (v) { return v; });
    

    Although you’ll need a QSA shim and a ES5 shim for legacy platform support.

    Edit:

    If you want the array to be unique then reduce it.

    var arr = arr.reduce(function (memo, val, key, arr) {
      // if the first index of the value is the index then add it.
      // if the first index is different then we already have it.
      if (arr.indexOf(val) === key) {
        memo.push(val);
      }
      return memo;
    }, []);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know do there have any way that I can encrypt the array
The numpy.equal function does not work if a list or array contains strings: >>>
var array = new Array(); $.get('comics.txt', function(data) { array = data.split(,); for(var i =
Why does it always return 49999 no matter what strToSearch variable holds? Even with
var arr1 = [12,'ss','sdd','sdd','kk']; function unique(array){ var o = {},b = []; for(var i=0;i<array.length;i++){
This command echo hello world | awk '{split($0, array, )} END{print length(array) }' does
Why does the array a not get initialized by global variable size ? #include<stdio.h>
When you do Something.find(array_of_ids) in Rails, the order of the resulting array does not
Which algorithm does the JavaScript Array#sort() function use? I understand that it can take
Does Scala have a version of Rubys' each_slice from the Array class?

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.