8

I have a JSON object as follows. After sending ajax call I want to clear this. How I can do?

var cfamFwdDtls = {
    cndtwizid: [],
    clientIdsInstnIds: [],
    positioncd: '',
    positionCnt: '',
    rcrtrInstnId: '',
    positionLocation: {
        cntryIdFrCndt: '',
        stateIdFrCndt: '',
        zipIdFrCndt: '',
        cityIdFrCndt: ''
    },
    searchPstnSkill: []
};
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
PSR
  • 39,804
  • 41
  • 111
  • 151
  • possible duplicate of [Deleting Objects in JavaScript](http://stackoverflow.com/questions/742623/deleting-objects-in-javascript) – John Dvorak Jul 15 '13 at 05:11

5 Answers5

11

If you want to reset the entire object, just reset the variable back to {};

cfamFwdDtls = {}; 
// or
cfamFwdDtls = new Object; 
// will prevent garbage collection
delete cfamFwdDtls;

However, if you want a more fine-grained way of "resetting" the object, you are going to need to define what specifically your requirements for a reset are. Regardless, you can always iterate through the object and make the necessary objects.

for (var key in cfamFwdDtls) {
    if (typeof cfamFwdDtls[key] == "string") {
        cfamFwdDtls[key] = '';
    } else if (Array.isArray(cfamFwdDtls[key])) {
        cfamFwdDtls[key] = [];
    } else {
        delete cfamFwdDtls[key];
    }
}

The above definition could be a possible way to define your particular situation since I only see strings and arrays in your object. If the key is neither of those, it would just delete the key. This could be tailored as you find necessary.

Shawn31313
  • 5,978
  • 4
  • 38
  • 80
  • You should strive for reference-quality writing on Stack Overflow, not informal forum posts... – user229044 Jul 15 '13 at 05:19
  • 1
    @Shawn31313 it is not working for me.If i use cfamFwdDtls = {}; i am losing all the fields for next time – PSR Jul 15 '13 at 05:37
  • Wait, you don't want to clear the whole object? So you want to clear each pair? – Shawn31313 Jul 15 '13 at 05:45
  • 1
    **The long way is not just for fun**. In the special case that the object, or parts of it, are referred to by multiple vars, then consistency among the multiple vars may require doing it the long way. – Paul Jul 15 '13 at 07:01
  • 1
    The "Just for fun" part helped me to find a way to only delete certain pairs depending on the key name. – Jelgab Jan 17 '14 at 03:26
  • 2
    -1. There are legitimate reasons to use the "long way". I agree that in this particular scenario it seems silly, but your answer needs to be refined to explain the situation better, instead of making the blanket statement "never do this". – James Watkins Jan 19 '16 at 16:32
  • @JamesWatkins I totally agree with you. I updated my answer! – Shawn31313 Mar 29 '16 at 16:53
3

for (var entry in cfamFwdDtls) delete cfamFwdDtls[entry];

If you simply reassign to {}, you'll get in trouble if there are multiple references to your object. Also may face garbage collection issues.

Nathan
  • 1,396
  • 3
  • 18
  • 32
1

there is alternative for this if you want to remove object.Something like this

delete cfamFwdDtls;

you can use delete keyword for deleting a object.

more details read

example

Community
  • 1
  • 1
Sonu Sindhu
  • 1,752
  • 15
  • 25
1
function getJson(){
   return {
            cndtwizid: [],
            clientIdsInstnIds: [],
            positioncd: '',
            positionCnt: '',
            rcrtrInstnId: '',
            positionLocation: {
                cntryIdFrCndt: '',
                stateIdFrCndt: '',
                zipIdFrCndt: '',
                cityIdFrCndt: ''
            },
            searchPstnSkill: []
         };
}

var data = getJson();
data.positioncd = 'xyz';
data.rcrtrInstnId = 'abc';

$.ajax(...){
}
success(response){
   data = getJson(); //re-initialize structure
}
0

The Problems

When I ran into this I wanted to solve two issues.

  1. Have one location for a complex structure definition.
  2. Reassurance the entire definition is reset.

Solutions

The basic idea is have a function return the empty structure. If it is not in a function you could change the structure itself not the instance of the structure.

Examples

Class

I personally use this but I also include the API functions in the class and make it an HTTP service.

class complexStructure {
  constructor () {
    this.payload = resetPayload();
  }
  resetPayload () {
    this.payload = {
      cndtwizid: [],
      clientIdsInstnIds: [],
      //...
    };
  }
}

Function

function resetStructure () {
  return {
    cndtwizid: [],
    clientIdsInstnIds: [],
    //...
  };
}

let resetStructure = resetStructure()
Michael Warner
  • 3,879
  • 3
  • 21
  • 45