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: []
};
1

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

7 Comments

You should strive for reference-quality writing on Stack Overflow, not informal forum posts...
@Shawn31313 it is not working for me.If i use cfamFwdDtls = {}; i am losing all the fields for next time
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.
The "Just for fun" part helped me to find a way to only delete certain pairs depending on the key name.
-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".
|
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.

Comments

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

3 Comments

That doesn't seem to be working. It only works when deleting individual key value pairs.
@Shawn31313 my answer show clearly that if you want to delete the object not only the value inside it.
it will delete entire "cfamFwdDtls" object
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
}

Comments

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.