0

I'm trying to create an array in Javascript with a size that is equivalent to the number of times a certain class is found in the DOM, and then iterate through it to grab the text from an input field present in that class. I can easily do this like so:

var count = 0;
$('.className').each(function() {
    count++;
});
var classes = new Array(count);
count = 0;
$('.className input[type=text]').each(function() {
    classes[count++] = $(this).val();
});

This looks like a lot of code for what seems to be a relatively simple task. Is there a more efficient or less lengthy way of doing this?

Thanks

5
  • 1
    There are obviously many possible improvements but I don't think this code does what you want it to do. Commented Jul 10, 2014 at 19:23
  • 2
    You don't need to initialize arrays, they're dynamic, just create an empty one, and start filling it up. Commented Jul 10, 2014 at 19:23
  • @dystroy it goes without saying that I am new to Javascript, however the code does currently work the way I want it to. Commented Jul 10, 2014 at 19:24
  • @MichaelParker: Then this sounds more like code review than stack overflow. Commented Jul 10, 2014 at 19:26
  • @MattBurland I didn't even know that existed. It does look like a better place to post this kind of question to. Commented Jul 10, 2014 at 19:28

5 Answers 5

6

It looks like you want this :

var classes = $('.className input[type=text]').map(function(){
    return this.value
}).get();

But it's a guess : it's not clear why you start by counting all elements of the class and then iterate on the inputs.

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

1 Comment

Yes, this is what I was looking for. The reason I counted the elements before creating my array was because I didn't know you could create arrays with an unspecified size. Still learning.
3

You can construct an array of elements directly from your selector via the makeArray function, then transform the result using a map.

var classes = $.makeArray($('.className input[type=text]')).map(function() {
    return $(this).val();
});

Comments

2

Use jQuery's map function, then get if you need a pure array:

var values = $('.className input[type=text]').map(function() {
    return $(this).val();
}).get();

Comments

1

each passes the index, so you don't need to do it yourself:

var classes = [];
$('.className input[type=text]').each(function(index, value) {
    classes[index] = $(this).val();
});

Comments

0

Arrays are dynamic and therefore don't need to be initialized. Create a new array, loop through the inputs and push the values to the new array:

var classes = [];
$('.className input[type=text]').each(function(idx, elem) {
    classes.push($(elem).val());
});

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.