Reputation: 863
Using jQuery, I'm attempting to iterate over an array and compare to a value, but am running into issues that are stumping me. Here's the code:
var cF = ['first','last','email'];
var fFound = 0;
cj(this).children('.section').each(function(f){
var fId = cj(this).attr('class');
fId = fId.replace('-section', '');
console.log('fId: ',fId);
cj.each(cF, function( cFi, cFv ){
console.log('cFv: ',cFv);
if ( cFv == fId ) {
console.log('found!');
fFound = 1;
}
});
console.log('fFound: ',fFound);
});
Basically what I'm doing is searching through a series of divs (passed with 'this'), retrieving the class name, stripping the suffix to the class, and then comparing it with my cF array. If found, I want to set fFound to 1 (it's my flag to indicate the value is present).
Originally I was using jQuery.inArray() but wasn't having much luck. When I send fId and cFv to the console -- they match -- but the comparison doesn't recognize them as matching.
Upvotes: 1
Views: 1223
Reputation: 55750
Try this
cj.each(cF, function( cFi, cFv ){
console.log('cFv: ',cFv);
if ( $.inArray( fId, cF ) > -1 ) {
console.log('found!');
fFound = 1;
}
});
Upvotes: 1
Reputation: 25776
var cF = ['first','last','email'];
var fFound = 0;
cj(this).children('.section').each(function(f){
var fId = cj(this).attr('class');
fFound = 0; // reset
fId = fId.replace('-section', '').trim();
console.log('fId: ',fId);
if( $.inArray( fId, cF ) !== -1 ) {
console.log('found!');
}
console.log('fFound: ',fFound);
});
Upvotes: 0
Reputation: 6232
iterate over array and compare values
var cF = ['first','last','email'];
var bla = 'anything'
var fFound = 0;
for (j in cF) {
if (bla.indexOf(cF[j]) > -1) {
fFound = 1;
}
}
ops... you want jQuery... scratch that.
Upvotes: 0