My code is outputting a series of objects to the console, like:
The code looks like the following:
var obj = <?php echo json_encode($paginas); ?>;
$('a.mod-articles-category-title').each(function () {
var links=$(this).attr('href');
if(links.indexOf('/')!=-1){
i=1;
}
else{
i=0;
}
var procura=links.match(/(\d+)/g)[i];
if(obj.indexOf(procura)!=-1){
console.log($(this));
$(this).addClass("visto");
};
});
Where obj is an array returned by a PHP function and procura is returning a number with a link id.
If the number in procura is in the obj array, I want to add the class "visto", to give it a different style, but it isn't working...
Edit: I missed where you said that obj is an array. There's a slightly different check for that. Just check to see if procura
is a valid index for the obj
array, and check that obj[procura]
exists.
Here's a JSFiddle: http://jsfiddle.net/rxgLC/
You're treating obj
like a string, calling indexOf
on it. If you are looking for a property on an object, use hasOwnProperty
if(obj.hasOwnProperty(procura)){
console.log($(this));
$(this).addClass("visto");
};