2

On my page I have a section containing UL's and each UL gets populated from an array of strings

dealers = [{
    title   : "Brecksville",
    address : "8383 Chippewa Rd.,",
    locale  : "Brecksville, Ohio - 44141",
    phone   : "(440) 740-0535",
    location: {lat: 41.32134300, lng: -81.62338300}
}];

For brevity I only show one of the strings in the array of strings...

This string is populated into the UL like so...

function populatDealerSummaryList(select, dealers) {
    $listSelector = $("#dealersList"); //The Dealer Summary List

    $.each(dealers, function (i, obj) {
        $listSelector.append(
            "<ul class='dealer-summary-list'>" +
            "<li class='dealerName'>" + obj.title + "</li>" +
            "<li class='dealer-summary-listItem'>" + obj.address + "</li>" +
            "<li class='dealer-summary-listItem'>" + obj.locale + "</li>" +
            "<li class='dealer-summary-listItem'>" + obj.location + "</li>" +
            "<li class='dealer-summary-listItem'>" + obj.phone + "</li>" +
            "</ul>");
    });
}

above...the latitude and longitude are represented by obj.location...

the problem is...the lat long only renders as [object, object]

I'm not sure how to pass it correctly to get it to render...please help

3
  • 1
    How would you expect it to be rendered…? Javascript doesn't know how to turn an object into a string either, other than [object Object]. You'll need to do that a little more manually. Commented Feb 17, 2017 at 12:42
  • try it in this way "<li class='dealer-summary-listItem'>" + obj.location.lat +"," +obj.location.lng+ "</li>" + Commented Feb 17, 2017 at 12:51
  • Here is a JSFiddle...jsfiddle.net/tonykiefer/hjwL0v1s Commented Feb 17, 2017 at 13:07

2 Answers 2

4

You get this result because Object method toString() return this output string "[object Object]";

You can write like this:

...>" + JSON.stringify(obj.location) + "<...

Or

...>" + obj.location.lat + ', ' + obj.location.lng + "<...

Look at JSFiddle. There are both approaches

Sign up to request clarification or add additional context in comments.

2 Comments

@tony Look at my answer again, I changed it. I added JQuery dependency to your JSFiddle and call initialize after document ready event.
Awesome Antonio...thanks for the help! I was going cross-eyed trying to figure this out.
1
obj.location is an object that contains two keys inside.

You pass in the <li> element the entire object.

You should just :

"<li class='dealer-summary-listItem'>" + obj.location.lat + " " + obj.location.lng + "</li>"

2 Comments

You use obj.location.lng twice. One should be obj.location.lat
@DionZac this doesnot work...please see the JSFiddle jsfiddle.net/tonykiefer/hjwL0v1s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.