I have two arrays containing string values (newValues and oldValues). I iterate over the oldValues array with jquery each and check the value against the newValues array. However even if the value exists in the newValues array, it is only found if I do a toString first. IE Debugger Console howerver says, that this is already a string (and thats what I expect too).
Code:
$(oldValues).each(function () {
// always fails (debug snapshot taken here)
if (jQuery.inArray(this, newValues) === -1) {
// ...
}
});

Why do I have to do a toString first, even when this is already of type string? Has this something to do with the jquery each?
All similar questions I’ve found had to do with type mismatch, but this isn’t the case here, right?
Use the following code instead:
Unless you use
"use strict";, the primitive string is boxed in aStringobject (see it yourself:typeof thisis"object", rather than string). Consequently, the search method (theindexOfmethod of an array) does not find the value.The above code solves this problem by correctly utilising
jQuery().each.