Your array contains two entries, but on this line:
if(uid == '0090000165'){
...you're comparing it to a string for just one of them. That will call toString
on the array, which will call Array#join
, which will give you the string "0090000163,0090000165"
. So you'll compare that with "0090000165"
. That's going to be false.
In your console screenshot, the second entry has the uid
you want, so you could do this:
if (uid[1] == "0090000165") {
// ^^^----- second entry, first would be at index 0
...but I suspect that since uid
contains multiple entries, you need to rework the logic, not just the comparison.
Re your answer to Felix's question:
Do you want to test whether the ID is in the array?
where you answered "yes". I'm going to assume you're building the uid
array for some other reason. You can do that by checking within your forEach
:
getParticipant(conf_url, function(data) {
var hasTheValue = false;
data.forEach(function(obj){
uid.push(obj['uid']);
if (uid == '0090000165') {
hasTheValue = true;
}
console.log(uid)
})
if(hasTheValue){
document.write("true");
}else{
document.write("false");
}
});
Or if you need to do it later, using only the array, you can use indexOf
:
if (uid.indexOf('0090000165') != -1) {
// yes it has it
}
If you don't need the array
for anything, then don't build it and just do:
getParticipant(conf_url, function(data) {
var hasTheValue = data.some(function(obj){
return obj['uid'] == '0090000165';
});
if(hasTheValue){
document.write("true");
}else{
document.write("false");
}
});
...but again, I assume you were building the array for something else as well (since it's declared outside the function).
Note: I would recommend not using uid
as the name of a property and of a variable where you expect a single value and also as the name of an array of them. But that could be my English language bias, I know not all languages differentiate between singular and plural nouns. If it's natural in your native language (I don't know if that's English or something else), ignore me.