0

consider i have an json array like this

["map14","map20","map21","map22","map23","map24","map25","map31","map32","map33","map34","map35","map36","map37","map40","map41","map42","map46","map49","map50"]

with this json array i need to check if my passed value exists need to do some operation or otherwise some other operation....

javascript code:

function pop(e,id) { 
$.getJSON( 'layoutcheck.php', { some_get_var: 1 }, function(output){
    var i=0, total = output.length;
    for ( i = 0; i < total; ++i ) {
if(isArray(output[i]==id)) {

// do soome stuff if the value exists in the database
      }
else{
// if not exists some other operation
 }
    }
});
}
</script>

layoutcheck.php will fetch the information from the database and create a json array..

but the code fails to display output.. please rectify me ..

thanks

4

2 回答 2

0

isArray()不会检查对象是否在数组中,而是检查对象是否是数组。

这意味着output[i]==idtruefalse)被选中。它们总是不是数组;这意味着您将始终进入else条件的一部分。

您可以尝试使用类似的东西:

if(output.indexOf(id) != -1) {
    // do soome stuff if the value exists in the database
}
else{
    // if not exists some other operation
}

而不是你的for循环。

于 2013-04-18T04:17:05.410 回答
0

如果您想知道是否id在数组output中,请使用 Javascript 的内置indexOf()方法:

if (output.indexOf(id) != -1) {
    // do soome stuff if the value exists in the database
} else {
    // if not exists some other operation
}

按照您编写它的方式,您将为不匹配else的每个值执行该子句。outputid

于 2013-04-18T04:24:47.650 回答