-2

I have my json. I have two object on it. enter image description here

uid 0090000165 is on my json but when i check it, it return false.

here is my code

var conf_url = "http://192.168.236.33//confbridge_participants/conference_participants.json?cid=0090000007";

 var uid = [];

    getParticipant(conf_url, function(data) {
        data.forEach(function(obj){
           uid.push(obj['uid']);

           console.log(uid)
        })
         
         if(uid == '0090000165'){
            document.write("true");
         }else{
             document.write("false");
         }
    });


 function getParticipant(conf_uri, handleData) {
    $.ajax({
      type: "GET",
      url: conf_uri,
      dataType: "jsonp",
      jsonpCallback: 'callback',
      contentType: "application/javascript",
      success: function(data) {
        handleData(data);
        //console.log(data);
      }
    });

  }

How can i check the value to be true ? Why it is returning false?

ar em
  • 434
  • 2
  • 5
  • 18
  • 5
    *"Why it is returning false?"* You are testing whether an **array** with multiple elements is equal to a **string**. Why do you expect it to return true? Would you expect `['foo', 'bar'] == 'foo'` to return true? What do you really want to test? – Felix Kling Aug 19 '15 at 06:12
  • 1
    This question is answerable by debugging the code with [debugging tools](http://webmasters.stackexchange.com/q/8525) available in your browser. Watch out for _errors_. [Rubber Duck Debug](http://rubberduckdebugging.com) your code. If you are not sure what your code does, use [`console.log`](https://developer.mozilla.org/en/docs/Web/API/Console/log) or [`debugger`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/debugger). Make sure your code is _valid_ with tools like [JSHint](http://jshint.com). Only _then_, ask a question on StackOverflow and _show your research_. – Sebastian Simon Aug 19 '15 at 06:14
  • Uhm sorry @Felix Kling. . I am beginner in programming. . how can check each value. . so that it will return true – ar em Aug 19 '15 at 06:14
  • 1
    Do you want to test whether the ID is **in** the array? – Felix Kling Aug 19 '15 at 06:14
  • yes @Felix Kling. . thats what i want to do. How will i do it? – ar em Aug 19 '15 at 06:18
  • @arem: I've added to my answer to address your response to Felix above. – T.J. Crowder Aug 19 '15 at 06:25
  • 1
    @FelixKling: Well, given the code above, he could also check in the loop. – T.J. Crowder Aug 19 '15 at 06:27

2 Answers2

3

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Its clearly you are pushing all uid of object into a array, so you have to check whether value of any of the item in your array is equal to your string value,

getParticipant(conf_url, function(data) {
    data.forEach(function(obj){
       uid.push(obj['uid']);

       console.log(uid)
    })

     if(uid[indexOfYourChoice] == '0090000165'){
        document.write("true");
     }else{
         document.write("false");
     }
});
nitin
  • 156
  • 2
  • 13