Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I Can't Write Objects in <li> Tag

When I writing my JSON data to

  • HTML tag it’s writing but don’t write object. It’s write [object Object]:

    â—‹Data1     â—‹wash dishes         â—‹[object Object]
               â—‹find some break     â—‹[object Object]
    

    I’m trying to do

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

  • lists with pointing JSON database but it’s looks like that. I’m calling my JSON with that code:

    var db_note = objpick(data);
    
    db_note.date.forEach(function(element) {
    tabledatednote.insertAdjacentHTML( 'beforeend',"<li>" + element + " </li>");
    /* I just writed to this part because is just changing "no_date.>date<" part while displaying other JSONs */
    });
    

    objpick.js is an external file

    function objpick(data){
    for(i in data){ var result = data[i] }
    return result
    };
    

    and there is my JSON database

    {
    "nodate": ["wash dishes", "find some bread"],
    "date": [{"01/01/2077" : "Cyberpunk Meet"}, {"25/05/2005" : "Buney?"}],
    "bookmark" : ["Data1"]
    }
    

    >Solution :

    Ultimately what’s being displayed is a string. So the code converts whatever you want to display to a string. For simple values, this conversion is easy. It’s consistent and obvious what the resulting string is for a single value.

    But objects are not simple values. They are potentially complex. And unless otherwise defined on your object(s), the default string representation for an object is: [object Object]

    Some of the things you are display are values:

    "find some bread"
    

    But some of them are objects:

    {"25/05/2005" : "Buney?"}
    

    For the objects, you’d need to tell the code how to display it. Some options I can think of are:

    • Manually. For example, check if a certain property exists on the object and, if it does, display that property’s value.
    • JSON-encode the object and display the resulting JSON string.
    • Override .toString() on your object(s).

    Which you choose is up to you, and for the same reason that you’re seeing the current default behavior… Because only you know how you want/expect that object to be displayed.

  • Add a comment

    Leave a Reply

    Keep Up to Date with the Most Important News

    By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

    Discover more from Dev solutions

    Subscribe now to keep reading and get access to the full archive.

    Continue reading