Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7727195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:23:57+00:00 2026-06-01T05:23:57+00:00

I want to add elements to arrays within an object that I access like

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:23:58+00:00Added an answer on June 1, 2026 at 5:23 am

    Answer was quite simple:

    Adding an object to an array of objects with splice

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add database elements into array. how do I do that. the
like array(a, b, c, d, e); I want to add new elements to it,
I've two arrays array1 and array2 and I want to add all elements of
I have an ArrayList full of strings arrays that I built like this: String[]
HI.. i want to add elements dynamically to listview in QT for symbian OS,
I want to add a class to multiple span elements with common classname's based
When you want to add whitespace between HTML elements (using CSS), to which element
I want add my custom sidebar next right column all page. Please check this
I have a list of string values that I want add to a hashtable
Ok, I need keys to be preserved within this array and I just want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.