jquery - Using substring of JSON key value for conditionals -


i have json data this:

   {   "events": [     {       "event": {         "event_instances": [           {             "event_instance": {               "id": 1365348,               "ranking": 0,               "event_id": 460956,               "start": "2015-07-07t00:00:00-05:00",               "end": null,               "all_day": true             }           }         ],         "id": 460956,         "title": "blue star museums",         "url": "http:\/\/www.samhoustonmemorialmuseum.com\/",         "updated_at": "2015-07-07t05:27:49-05:00",         "created_at": "2015-06-02t12:34:01-05:00",         "facebook_id": null,         "first_date": "2015-06-02",         //so on , forth 

i'm need use first_date key value in jquery conditional this:

if(first_date.value().substring(0,3) === 2015){     //do } 

can use substrings of key values conditionals in jquery?

so assuming that json assigned variable named jsonvariable

var first_date = jsonvariable.events[0].event.first_date; var first_date_year = first_date.substring(0,4);  if (first_date_year === '2015') {     // somethang } 

how access first_date

first access first_date have access array events, first element of array (using index [0]), event property, , sub property first_date

how take substring

to take substring need use substr(0,4) because second parameter length in characters of substring not index position end it.

how compare years

then compare substring 2015 need wrap 2015 in quotes convert string, or use parseint() convert substring integer.

note: split string @ - , use first element

var first_date = jsonvariable.events[0].event.first_date; var first_date_year = first_date.split('-').pop(); // ["2015", "06", "02"] pop first value 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -