0

I'm trying to set up an array through jQuery's .each() function, but it seems like I'm not properly doing it?

I have attributes in the html such as:

<div class="cheers" data-fname = "fname" data-lname="lname">some ish..</div><!-- going through a while loop!-->

Then I have a jquery function that does something like this

var arrayMe = [];
$(".cheers").each(function(index){
    arrayMe[index] = $(".cheers").attr('data-fname')+","+$(".cheers").attr('data-lname');
});

Then, when I try to do various alerts:

alert(arrayMe); //this gives me the fname,lname
alert(arrayMe[0]); //this gives me the first fname,lname in the array
alert(arrayMe[0][1]); //this SUPPOSED to give me the first lname, but it gives me a letter...

5 Answers 5

6

You have to use arrayMe[1] instead of arrayMe[0][1].

You get a letter, because arrayMe[0] is a string, and arrayMe[0][1] retrieves the second character of the given string. It's equivalent to arrayMe[0].charAt(1).

If you want to build a 2D array, use:

var arrayMe = [];
$(".cheers").each(function(index){
    var $this = $(this);
    arrayMe[index] = [$this.attr('data-fname'),
                      $this.attr('data-lname')];
});
alert(arrayMe);        // Array. Shows all pairs, eg: [['fname', 'lname'], ...]
alert(arrayMe[0]);     // Array. Shows first pair, eg: ['fname', 'lname']
alert(arrayMe[0][0]);  // String. fname            eg:  'fname'
alert(arrayMe[0][1]);  // String. lname            eg:           'lname'

(I have also fixed another issue in your code by replacing $('.cheers') with $(this))

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to access fname and lname separately, put them in an array:

var arrayMe = [];
$(".cheers").each(function(index){
    arrayMe[index] = [$(this).attr('data-fname'),$(this).attr('data-lname')];
});

Also, $(this).attr will give you the attribute of the current element, where $(".cheers").attr will always give you the attribute of the first element

Comments

1

What about this:

var arrayMe = $(".cheers").map(function() {
    return [[ $(this).data('fname'), $(this).data('lname') ]];
}).get();

See squint's comment for explanation why double return-array needed.

4 Comments

.map() has a strange quirk where if you return an Array, instead of being added to the Array being built, it instead is concatenated into it. To overcome this, you need to wrap the return value in yet another Array so that that's the one that gets concatenated. return [ [ ..., ... ] ];
Wow! this is interesting. Just tested and you are right!! Thank you, I didn't know about that.. now looking into jQuery source code, interesting..
Additionally, .map returns a jQuery object. To convert it to an array, you have to add .toArray() in the end.
FYI, here's a bug report. They have no intention of fixing it. .map was an internal use function that they decided to expose irrespective of a few things that are unexpected for .map. Unfortunte. +1 though.
0

From what I can see you are not adding the values into an array, but instead into a string which is being stored in arrayMe[index].

Try using when storing into the array to achieve what I believe you want:

var arrayMe = [];
$(".cheers").each(function(index){
    arrayMe[index][0] = $(".cheers").attr('data-fname');
    arrayMe[index][1] = $(".cheers").attr('data-lname');
});

Comments

0

You will have to change your code to:

arrayMe[index] = [$(".cheers").attr('data-fname'), $(".cheers").attr('data-lname')];

Comments

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.