Javascript [SOLVED]: Query JSON data sharing same value

Javascript [SOLVED]: Query JSON data sharing same value

Home Forums Scripting Javascript Tutorials Javascript [SOLVED]: Query JSON data sharing same value

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245694

    Cloudy Point
    Keymaster

    QuestionQuestion

    Sample JSON Data:

    { 
    "results": [
      {
        "name": "John Smith", 
        "state": "NY", 
        "phone": "555-555-1111"
      },
      {
        "name": "Mary Jones", 
        "state": "PA", 
        "phone": "555-555-2222"
      }, 
      {
        "name": "Edward Edwards", 
        "state": "NY", 
        "phone": "555-555-3333"
      }, 
      {
        "name": "Abby Abberson", 
        "state": "RI", 
        "phone": "555-555-4444"
      }, 
    ]}

    With this sample data I can display individual values from the results [] array with object.name and object.phone to look something like:

    John Smith 555-555-1111<br />
    Mary Jones 555-555-2222<br />
    Edward Edwards 555-555-3333<br />
    Abby Abberson 555-555-4444
    

    What I am trying to do now is select just the people who’s state value is NY and only display their object.name and object.phone:

    John Smith 555-555-1111<br />
    Edward Edwards 555-555-3333
    

    I tried this lovely little block but all it did was print all the names, which makes sense after I tried it.

    if (object.state = "NY") {
          div.append(repName);
    }
    

    I can’t seem to think of a way to only display those that share a the same state.

    I’m probably searching for the wrong terms or have to go about this another way… please help!

    #245695

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You are using =(assignment operator),which is wrong.

    You have to use ==(comparison operator)

    So do like below:-

    if (object.state == "NY") {
       div.append(repName);
    }
    

    Working sample-

    var obj = { 
    "results": [
      {
        "name": "John Smith", 
        "state": "NY", 
        "phone": "555-555-1111"
      },
      {
        "name": "Mary Jones", 
        "state": "PA", 
        "phone": "555-555-2222"
      }, 
      {
        "name": "Edward Edwards", 
        "state": "NY", 
        "phone": "555-555-3333"
      }, 
      {
        "name": "Abby Abberson", 
        "state": "RI", 
        "phone": "555-555-4444"
      }, 
    ]};
    
    
    $(obj.results).each(function(k,object){
      if (object.state == "NY") {
        $('#final_data').append(object.name +" : "+object.phone+"<br/>");
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="final_data"></div>

    Source: https://stackoverflow.com/questions/48025729/query-json-data-sharing-same-value
    Author: Alive to Die
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.