1

I have the following json (extract):

[{"series":{"series_long_name":"Micky mouse","id":"17684"},"0":{"listings":"3"}}]

I need to reference the number of listings to populate and autocomplete:

.data( "ui-autocomplete" )._renderItem = function( ul, item ) {

        return $( "<li>" )
        .append( '<a>' + item.series.series_long_name + '<span class="listings-count">' + item.0.listings + '</span></a>' )
        .appendTo( ul );
    };  

but using item.0.listings throws an error, I guess because I can not add an int as an object key?

I believe that the 0 is being returned as it is a count().

Here is an extract array returned before being encoded:

(int) 0 => array(
        'series' => array(
            'series_long_name' => 'Micky mouse',
            'id' => '17684'
        ),
        (int) 0 => array(
            'listings' => '3'
        )
    ),

Any idea how best to tackle this?

Man Programmer
  • 5,300
  • 2
  • 21
  • 21
squeaker
  • 395
  • 2
  • 7
  • 17

1 Answers1

0

Simply use bracket notation to access the field. Ex: item[0] or item['0'].

See this answer for a more complete explanation: How to access a numeric property?

Community
  • 1
  • 1
HotStuff68
  • 925
  • 1
  • 10
  • 13