Repurposing JavaScript objects after clearing their contents

Here's my issue. I'm working with a Javascript object, initialized as var stack = {}. This object is used in my project to store arrays. When the user clicks the add button, an array is added to the object with a specific key that is inputted in an HTML field. The user can click the add button multiple times to add more arrays. Upon clicking the submit button, the data is sent via ajax to php. After a successful response, the stack is cleared and ready for use again. Below are sample functions showcasing this implementation.

Add.js

function add(stack){
      ...
      var arr = ['apple','oranges'];
      stack[someName] = arr; 
}

Submit.js

function submit(stack){
      $.ajax({ 
            type: "POST",
            url: "somefile.php",
            success: function(html){
                //some event
                ...
                //remove elements from stack
                stack = {};
            }
       });
}

Main.js

var stack = {};
$('#addButtonId').on('click',function(){
    add(stack);
}
$('#sumbitButtonId').on('click',function(){
    submit(stack);
}

The issue arises after clicking the submit button and clearing the object. If the add button is clicked again, the object stack doesn't hold any new arrays. Can anyone shed light on what might be happening? I've heard about potentially leaving garbage when clearing objects with obj = {}, but I'm unsure if that relates to my problem.

Appreciate any help in advance!

Answer №1

Give this a shot.

for (var member in stack) {
  delete stack[member];
} 

When you use the code stack = {}, it creates a new empty object rather than clearing its existing contents. This should technically work fine, so there might be an error elsewhere in your code that needs to be addressed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue encountered with Angular when making a post request, whereas there is no problem with J

I am attempting to send a post request containing information to a token Uri. This information should result in receiving an access token back from the token Uri once authorization is granted. I have successfully implemented this on a plain HTML page using ...

The feature of containment does not function properly with Draggable and Resizable elements

I'm trying to create a resizable and draggable div within a larger div that is contained in a smaller div with a scroll bar. Here's the structure: <div id="hgcScroll" style="width:600px;overflow:auto"> <div id="hgcRegle" style="wid ...

The PHP file on my local WAMP server seems to be having trouble retrieving inputs from the HTML's GET or POST methods

<!DOCTYPE HTML> <html> <body> <form action="testrun.php" method="GET"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </bo ...

assigned to a variable and accessed in a different route

Why does the "res.username" variable return as undefined in the second route even though my user needs to login before accessing any route? router.post('/login', passport.authenticate('local'), function(req, res) { res.username = r ...

Pause the audio using jQuery when the slide changes

I have a Drupal website with a slider that includes an audio player on each slide. I need the audio to stop whenever the slide changes. The slider plugin I'm using is owlCarousel. Here is my current code: $("#owl-demo").owlCarousel({ afterAction: ...

Uniform masonry grid height

Looking to customize the Masonry script for displaying images similar to those seen on this webpage: Do you think it's possible? Alternatively, do you recommend any other libraries that might work better? Thank you in advance for your assistance. ...

utilizing vue model attributes with `${}`

I am encountering an issue with my code. Initially, :src="labels[index]" was working fine for me. However, as my codebase expanded, I needed to use ${app.labels[index]}. Simply using ${app.labels} works, but the addition of [index] causes it to b ...

Transferring data between jQuery and other global JavaScript variables

I'm facing a challenge when trying to make functions I created in jQuery access JavaScript values defined elsewhere. For instance, I have a function set within my jQuery code. var parentImg = ''; //global variable. $(document).change(funct ...

Tips for creating a hover effect on an icon within a CSS grid

I've just started learning to code and wanted to create a component for previewing/displaying a project I'm working on. I've been attempting to add a hover effect to my links such as the GitHubIcon and LaunchIcon, but so far, it's not w ...

Is there a way to retrieve the dynamically generated text content of an element using document.write?

I am encountering an issue similar to the following: <div id="myDiv"><script>document.write('SOMETHING');</script></div> My goal is to extract the content inside the script tag, which in this case is "SOMETHING" ...

Ensuring Contact Form Accuracy with Jquery Validation

I am trying to implement form validation using jQuery. Here is the code I am using: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2 /jquery.min.js"></script> <script type="text/javascript" src=" http:// ...

The functionality of the jQuery scroll feature appears to be malfunctioning

I am trying to implement a scroll event and have written the following code: <script type="text/javascript> $(function () { $(window).scroll(function () { alert($("body").scrollTop()); }); }); ...

Exploring the possibilities of PhoneGap, leveraging the power of jQuery getJSON

I'm currently working on a mobile application using PhoneGap that requires AJAX functionality with jQuery. However, I've encountered an issue when trying to retrieve a JSON from a PHP web service with CORS enabled. The strange thing is that the c ...

Troubleshooting issues with JavaScript events in order to effectively implement popovers

I am facing an issue on a webpage that contains a significant amount of JavaScript. The Twitter bootstrap's popover widget is not functioning as expected. Specifically, when I hover over the icon that should trigger the "popover," nothing happens. I h ...

How to Set an Iframe Target with Ajax and Jquery?

I am working with zend-framework, iframes, ajax, and jquery to call a URL when a specific DIV is clicked. Here's how I am using an iframe: <iframe src="http://localhost.test.com/public/index/listen-message" name="listenMsg" height="120" width="60 ...

Issue encountered with JavaScript function within TypeScript file connected to HTML code

I am currently working on a simple SharePoint web part and encountering an issue with using a function from another module file in my main file. Snippet from the JSFunctions.module.js file (where I define my function): function getApi(){ [my code]... }; ...

Issue with Bootstrap modal not closing

I've encountered an issue with a bootstrap modal popup. When I try to close the popup, it doesn't behave as expected. Instead of just hiding the popup and removing the backdrop, it hides the popup but adds another backdrop, making the screen almo ...

Tips for shifting a div to the left with AngularJS

I am working with a div structure that looks like the following: <div class="col-xs-1 scroll-button"><i class="glyphicon glyphicon-chevron-left"></i> </div> <div class="customer-ust-bant col-xs-22" id="letters"> <box n ...

Creating a dynamic 3D pie chart with Highcharts and JSON data

I am attempting to create a 3D pie chart using HighChart with JSON data. Despite enabling the 3D option, it only displays in 2D. Here is an example of the 3D pie chart I am trying to achieve: Could someone please help me identify what might be missing in ...

Tips for regularly retrieving information from a psql table

I have a scenario where I am retrieving data from a psql table and converting it into a JSON array to be used for displaying a time series chart using JavaScript. The data that is passed needs to be in the form of an array. Since the data in the table get ...