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 7551531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:31:14+00:00 2026-05-30T10:31:14+00:00

I initially receive an AJAX response of {B:{1:100,3:{AA:256}},A:100} and converted to a javascript object:

  • 0

I initially receive an AJAX response of {"B":{"1":"100","3":{"AA":256}},"A":100} and converted to a javascript object:

var jsonOBJ = {};
jsonOBJ = jQuery.parseJSON(data);

Future responses can be subsets or supersets of the initial response. If the value of a table is unchanged at the server, the stagnant data is replaced with an empty array. Example:

{"B":{"1":"90","2":200,"3":[]}}

{"B":[],"A":20}

Everytime an AJAX response is received, the object is updated with:

jQuery.extend(true, jsonOBJ, jQuery.parseJSON(data));

But I need the javascript object to keep the unchanged portions, so I need to end up with an object that would be equivalent to the following with the example responses above:

jsonOBJ = jQuery.parseJSON('{"B":{"1":"90","2":200,"3":{"AA":256}},"A":20}');

My preferred option would be to remove the empty objects from the converted response. Is there an existing function or a modification to the jQuery extend function that would do this?

  • 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-05-30T10:31:15+00:00Added an answer on May 30, 2026 at 10:31 am

    You can remove the elements in your response with empty arrays with this code.

    It cycles through the top level, looking for any empty arrays and removing them. Any objects it finds, it recurses into to also remove empty arrays in them:

    // make sure the ES5 Array.isArray() method exists
    if(!Array.isArray) {
      Array.isArray = function (arg) {
        return Object.prototype.toString.call(arg) == '[object Array]';
      };
    }
    
    function removeEmptyArrays(data) {
        for (var key in data) {
            var item = data[key];
            // see if this item is an array
            if (Array.isArray(item)) {
                // see if the array is empty
                if (item.length == 0) {
                    // remove this item from the parent object
                    delete data[key];
                }
            // if this item is an object, then recurse into it 
            // to remove empty arrays in it too
            } else if (typeof item == "object") {
                removeEmptyArrays(item);
            }
        }    
    }
    
    var jsonOBJ = {};
    jsonOBJ = jQuery.parseJSON(data);
    removeEmptyArrays(jsonOBJ);
    

    You can see it work here: http://jsfiddle.net/jfriend00/U6qMH/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a bit of a hairy issue when submitting data over a jquery ajax
Initially I had a method in our DL that would take in the object
I'm trying to build DOM with jQuery and fill it with data that is
I have a javascript function called GetRequest() that calls the server with $.ajax() and
Eric Willis' response to this question: https://stackoverflow.com/questions/563041/receive-emails-in-a-net-service-c prompted me to start thinking of creating
Background i'm using jquery.sparkline to produce Pie Charts . The data for the Pie
Initially I thought this was going to work, but now I understand it won't
Initially I deployed packages on SQL server but since my machine is not having
Initially I wanted a checkmark where the text is placed on the left of
Initially i was planning to use master page for every page in my application.

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.