1

I want to add elements to arrays within an object that I access like this to retrieve data.

var event_id = events_data.event_id[i]["0"];
var event_title = events_data.event_title[i]["0"];
var selected_source = events_data.selected_source[i]["0"];
var channel_id = events_data.channel_id[i]["0"];

events_data is an object with elements event_id, event_title, ... That object is created in a php function like this.

$return = array();
$return['event_id'] = $event_id; 
$return['event_title'] = $event_title;
$return['selected_source'] = $selected_source;
$return['channel_id'] = $channel_id;
$return['channel_name'] = $channel_name;
$return['event_site'] = $event_site;
$return['event_url'] = $event_url;
$return['start_date'] = $start_date;
$return['start_time'] = $start_time;
$return['end_date'] = $end_date;
$return['end_time'] = $end_time;
$return['event_notes'] = $event_notes;

echo json_encode($return);

EDIT - ADDED INFO

The json object looks like this.

{"event_id":[{"0":"e20120319215556"},{"0":"e20120310221512"},{"0":"e20120319151903"},{"0":"e20120309123705"},{"0":"e20120307122044"},{"0":"e20120306182514"},{"0":"e20120309211714"},{"0":"e20120314130727"},{"0":"e20120319150532"},{"0":"e20120319141928"},{"0":"e20120319141201"},{"0":"e20120301193226"},{"0":"e20120301184354"}]}

END INFO ADDED

On the javascript side I get the events_data array like this.

$.ajax({
    url: "get_events_data.php",
    type: "POST",
    dataType : 'json',
    data: { },
    cache: false,
    async: false,
    success: function (rdata) {
        events_data = rdata;
                  }
         });

To add an element to events_data on the javascript side, I check for the index where to add the new element and then add it with splice. But since the data is retrieved with the ["0"] text key for the associative array, I don't know how to specify the splice parameters.

for ( var n=0; n<events_data.event_id.length; n++ ) { 
   if ( current_event_id == events_data.event_id[n]["0"] ) {
       //splice_index = n;              
       events_data.event_id.splice(n,0,event_id);
       events_data.event_title.splice(n,0,event_title);
       events_data.selected_source.splice(n,0,selected_source);
       events_data.channel_id.splice(n,0,channel_id);
       events_data.channel_name.splice(n,0,channel_text);
       events_data.event_site.splice(n,0,event_site);
       events_data.event_url.splice(n,0,event_url);
       events_data.start_date.splice(n,0,start_date_string);
       events_data.start_time.splice(n,0,start_time_string);
       events_data.end_date.splice(n,0,end_date_string);
       events_data.end_time.splice(n,0,end_time_string);
       events_data.event_notes.splice(n,0,event_notes);
       break;
    }
}

Specifying the splice index "n" doesn't seem to do it. What should I change?

Thanks.

4

1 回答 1

1

答案很简单:

使用拼接将对象添加到对象数组中

var add_object = {"0",event_id}; // declare event id as object
events_data.event_id.splice(n,0,add_object); // splice object into object array at index
于 2012-03-30T18:48:40.403 回答