I have a PersonVO
object having fields firstname
, lastname
, id
.
I am preparing an Arraylist
of PersonVO
objects in backend with the help of Json API, and I want to display it in frontend with the help of jQuery.
How to display only specific fields 开发者_高级运维without hard-coding the fields in frontend like this.firstname
or this.id
or this.lastname
, possibly hiding the id
field?
You can use $.each to traverse through all object properties.
Example: http://jsfiddle.net/u78Ce/
Edit: I think you meant to save the keys somewhere in your program, and later access that key of an Object. In javascript, for objects, a.var is the same as a['var']. So you could do something like
var first_name_column = 'firstName', last_name_column = 'secondName';
Then later in your code, access them like
var person = { firstName: "John", lastName: "Doe" };
var fn = person[first_name_column], ln = person[last_name_column];
精彩评论