Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strings in array are no longer strings after jQuery.each()

I'm pretty confused with the behaviour of arrays of strings when I loop them through the jQuery.each() method. Apparently, the strings become jQuery objects inside the callback function. However, I cannot use the this.get() method to obtain the original string; doing so triggers a this.get is not a function error message. I suppose the reason is that it's not a DOM node. I can do $(this).get() but it makes my string become an array (from "foo" to ["f", "o", "o"]).

How can I cast it back to string? I need to get a variable of String type because I pass it to other functions that compare the values among them.

I enclose a self-contained test case (requires Firebug's console):

<!DOCTYPE html>
<html>
<head><title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript"><!--
$(function(){
    var foo = [];
    var $foo = $(foo);

    foo.push("987");
    $foo.push("987");

    foo.push("654");
    $foo.push("654");

    $.each(foo, function(i){
        console.log("foo[%d]: object=%o; value=%s; string=%o", i, this, this, $(this).get()); // this.get() does not exist
    });
    $foo.each(function(i){
        console.log("$foo[%d]: object=%o; value=%s; string=%o", i, this, this, $(this).get()); // this.get() does not exist
    });
});
//--></script>
</head>
<body>

</body>
</html>
like image 435
Álvaro González Avatar asked Mar 26 '10 11:03

Álvaro González


People also ask

What is each() in jQuery?

jQuery Misc each() Method The each() method specifies a function to run for each matched element. Tip: return false can be used to stop the loop early.

How to iterate string array in jQuery?

each(), which is used to iterate, exclusively, over a jQuery object. The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

How to break jQuery each loop?

To break a $. each or $(selector). each loop, you have to return false in the loop callback. Returning true skips to the next iteration, equivalent to a continue in a normal loop.

How to each array in jQuery?

Answer: Use the jQuery. each() function each() or $. each() can be used to seamlessly iterate over any collection, whether it is an object or an array. However, since the $. each() function internally retrieves and uses the length property of the passed array or object.


Video Answer


1 Answers

edit/clarification: calllback functions takes two arguments, the second one is value of the variable. Use it instead of this I have tested both variants and everything seems to work as expected (strings are still strings).

>>> $.each(['foo','bar'], function(i, s){ console.info( s, typeof s ) })
foo string
bar string

>>> $(['foo','bar']).each(function(i, s){ console.info( s, typeof s ) })
foo string
bar string
like image 122
pawel Avatar answered Nov 15 '22 22:11

pawel