jquery - Getting total count of JSON response -
can done in jquery before start looping through them? or need setup separate group in response of data total count lets say.. first variable?
the common response looks this:
[ {"name":"tu\u011frul","surname":"topuz","message":"hello world"} ,{"name":"tu\u011frul","surname":"topuz","message":"hello world"} ]
but use jquery return 2
total count (before loop through them all?) or need this?
[ {"total":"2"} ,{"name":"tu\u011frul","surname":"topuz","message":"hello world"} ,{"name":"tu\u011frul","surname":"topuz","message":"hello world"} ]
whats efficient/common (optimization-wise) way of doing this?
as after json decoding getting array of objects(from have posted). can access length
property of total count.
as have got array, assume have been able parse json response js object. can use datatype: 'json'
tell jquery automatically or can parse using jquery.parsejson()
necessary if set datatype:'json'
or set content-type
header application/json
server.
so can this
$.ajax({ .... datatype: 'json', success(function(data){ if(data){ var total = data.length; } } });
so dont need send total count
server. can js after getting response , make easier traverse response using $.each
whole array contains same type of objects (doesn't have special {"total":"2"}
member).
note: length
property pure vanilla js, has nothing jquery
.
Comments
Post a Comment