2

I am trying to display the items in an array with jQuery but it's just returning [object] [Object]. What am I doing wrong here?

There data is returned in this format:

Object
1:{gps: "0.000,0.000", street_name: "First Street", street_name_alt: "1st Street"}
2:{gps: "0.000,0.000", street_name: "Second Street", street_name_alt: "2nd Street"}

And I am trying to output it with:

$.each( data.street, function( key, val ) {                       
        htm+='<ons-list-item id="'+key+'">';
        htm+=' '+val;
        htm+='</ons-list-item>';
});

2 Answers 2

2

val is your object and key is index..

You have to try accessing it's properties. For ex to access street_name

$.each( data.street, function( key, val ) {                       
        htm+='<ons-list-item id="'+key+'">';
        htm+=' '+val.street_name;
        htm+='</ons-list-item>';
});

It's not a multi dimensional array btw, it's a nested object.

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

1 Comment

Ooooh, I see what I did wrong. It's working now! Thank you :)
0

data = {};
data.street = [{gps: "0.000,0.000", street_name: "First Street", street_name_alt: "1st Street"},
{gps: "0.000,0.000", street_name: "Second Street", street_name_alt: "2nd Street"}];

var htm = "";
// updated the arguments to reflect their functions better.
$.each( data.street, function( index, element ) {                       
        htm+='<ons-list-item id="'+index+'">';
        htm+=' '+element .street_name;
        htm+='</ons-list-item>';
});
console.log(htm);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

That's because val refers to the full object. Use the property names to get the right result.

Comments

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.