Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear JSON object using JavaScript or jQuery

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: []
};
like image 559
PSR Avatar asked Jul 15 '13 04:07

PSR


1 Answers

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.

like image 54
Shawn31313 Avatar answered Oct 16 '22 01:10

Shawn31313