SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Splice string into array

    I have a array that comes from PHP as a JSON array. I'd like to splice new items in the middle of the array. The code I have to do this is this one:

    Code:
                                    var k = 1;
      				if ( new_event_date > cur_event_date ) {
    					var splice_index = k - 1;
    					$(this).before(print_nav);
    					var current_event_id = $(this).attr('id');
    					current_event_id = current_event_id.substr(2);
    					alert ('event id '+event_id+' current id '+current_event_id);
    					for ( var n=0; n<events_data.event_id.length; n++ ) { 
    						if ( current_event_id == events_data.event_id[n]["0"] ) {
    							events_data.event_id.splice(splice_index,0,event_id);
    							alert('save event id '+event_id+' splice_index '+n+' data event id '+events_data.event_id[n]["0"]);
    							events_data.event_title.splice(splice_index,0,event_title);
    							alert('save event id '+event_id+' splice_index '+n+' data event title '+events_data.event_title[n]["0"]);
    							events_data.selected_source.splice(splice_index,0,selected_source);
    							events_data.channel_id.splice(splice_index,0,channel_id);
    							events_data.channel_name.splice(splice_index,0,channel_text);
    							events_data.event_site.splice(splice_index,0,event_site);
    							events_data.event_url.splice(splice_index,0,event_url);
    							events_data.start_date.splice(splice_index,0,start_date_string);
    							events_data.start_time.splice(splice_index,0,start_time_string);
    							events_data.end_date.splice(splice_index,0,end_date_string);
    							events_data.end_time.splice(splice_index,0,end_time_string);
    							events_data.event_notes.splice(splice_index,0,event_notes);
    							break;
    						}
    					}
    					return false;
    				}
    events.data is a json array of arrays from PHP. The splice method only seems to put the first char of the string within the new item in the array. For example, if for event_id e901, events_data.event_id[splice_index]["0"] only prints "e", similarly for the events_data.event_title. What must I fix in the script to put the entire string?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mmmosias View Post
    if for event_id e901, events_data.event_id[splice_index]["0"] only prints "e", similarly for the events_data.event_title. What must I fix in the script to put the entire string?
    It seems that events_data.event_id[splice_index] is giving you "e901".
    JavaScript also uses array indexes to retrieve a char from a string, which is why "e901"[0] is giving you the first char from that string.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •