1

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
  • Who downvoted this? It's a perfectly valid question (I upvoted back to zero) Commented Nov 6, 2011 at 1:44

3 Answers 3

3

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;
}, []);
Sign up to request clarification or add additional context in comments.

7 Comments

While I agree the OP should be using [] instead of new Array(), would you say the former is more readable (especially for a beginner)?
Your jQuery code would return a different set then the OP's code (it doesn't add falsy emails), and would also return a jQuery object when the OP wants an array. jsFiddle.
@alex I fixed that. I made the poor assumption that .map behaves similarly to ES5 rather then doing is this jQuery specific nonsense.
@Raynos jQuery's map() working this way lets you easily modify the jQuery collection. It's slightly hacky to use it to return other data types and then toss a get() at the end.
@alex I don't approve. The API is a mess and non-intuitive, but that's an aside. I guess you would want [].map.call(jQueryObj, mapper)
|
1

You could do all of that using a few jQuery methods.

var emails = $('.emailBox input')
              .map(function() { return $(this).val() || null; })
              .get();

jsFiddle.

Comments

-1

emails[email] = email isn't doing what you want it to do. Try

emails.push(email);

4 Comments

Why isn't it? It works fine for me. It may not be what the OP wants, but it's perfectly valid.
If emails was an object, then it would work (as you're setting a key to have a value), but it's an array which has numeric indexes. I don't see what it would do with an email address as an index? (Although you could argue that an Array is an object too :) )
Store it, then look it up by the same--it's just a property. Arrays are objects--try it.
@Todd of course an array is an object. Everything is an object (apart from null and undefined)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.