简体   繁体   English

如何使用 JavaScript 或 jQuery 清除 JSON 对象

[英]How to clear JSON object using JavaScript or jQuery

I have a JSON object as follows.我有一个 JSON 对象,如下所示。 After sending ajax call I want to clear this.发送 ajax 调用后,我想清除它。 How I can do?我能怎么办?

var cfamFwdDtls = {
    cndtwizid: [],
    clientIdsInstnIds: [],
    positioncd: '',
    positionCnt: '',
    rcrtrInstnId: '',
    positionLocation: {
        cntryIdFrCndt: '',
        stateIdFrCndt: '',
        zipIdFrCndt: '',
        cityIdFrCndt: ''
    },
    searchPstnSkill: []
};

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.这可以根据您的需要进行定制。

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.也可能面临垃圾收集问题。

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.您可以使用 delete 关键字删除对象。

more details read更多详情阅读

example 例子

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.我个人使用它,但我也在类中包含 API 函数并使其成为 HTTP 服务。

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

Function功能

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

let resetStructure = resetStructure()
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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM