Other posters have suggested workarounds, I'll try to answer the question why your original code doesn't work. The answer is rather non-trivial and reveals some good-to-know javascript gotchas.
jQuery.each uses apply to pass this to its callback. When apply's argument is a primitive value, like string, it will be "boxed", i.e. converted to an object (specifically, the String object):
console.log(typeof("cat")) // string
logger = function() { console.log(typeof(this)) }
logger.apply("cat") // object
Now consider the following:
a = ["abc"]
b = ["abc"]
$(a).each(function() {
var that = this
$(b).each(function() {
console.log(this == that) // false!
})
})
Although a[0] and b[0] are "obviously" equal, the == operator returns false because both are objects, and two object variables are only equal if they are physically the same object. On the other side, this works as expected:
a = ["abc"]
b = ["abc"]
$(a).each(function() {
console.log(this == "abc") // true
console.log(this == b[0]) // true
})
When JS compares an object to a string, the object is converted to a string primitive using toString. Since this is a String object, its toString returns the primitive string it was made of, and two primitive strings are equal if their characters are equal.