如何使用Javascript从sails.js中的JSON对象中删除“NULL”字符串?

时间:2015-02-11 04:57:59

标签: javascript json sails.js

JSON对象如下所示。我想删除值为NULL的所有键。此外,如果在删除该对后,如果该条目为空,那么也应该删除该条目。我曾尝试寻找执行所需任务的脚本,但我没有成功。请记住,它是一个NULL字符串,而不是一个对象。我需要实现这个是sails.js所以Javascript是最受欢迎的语言。

var JSONobj ={  "regex_attributes": {
"matching attributes": {
  "transaction_amount": {
    "matching_position": "1"
  },
  "total_amount_due": {
    "matching_position": "NULL"
  },
  "transaction_date": {
    "matching_position": "NULL",
    "date_format": "NULL"
  },
  "current_credit": {
    "matching_position": "NULL"
  },
  "available_credit": {
    "matching_position": "NULL"
  },
  "date_posted": {
    "matching_position": "NULL",
    "date_format": "NULL"
  },
  "merchant": {
    "matching_position": "NULL"
  },
  "bill_date": {
    "matching_position": "NULL",
    "date_format": "NULL"
  },
  "bill_due_date": {
    "matching_position": "NULL",
    "date_format": "NULL"
  },
  "bill_amount": {
    "matching_position": "NULL"
  },
  "account_number_ending": {
    "matching_position": "2"
  },
  "balance_date": {
    "matching_position": "NULL",
    "date_format": "NULL"
  },
  "available_balance": {
    "matching_position": "NULL"
  }
},
"additional_processing": [     //Array
  {
    "sms_sender_name": "NULL",
    "type_of_account_to_find": "NULL",
    "attribute": "description",
    "operation": "pre_add",
    "pre_add_1": "Credit"
  }
],
"account_details": {
  "credit_or_debit": "CREDIT",
  "type_of_transaction": "INT",
  "type_of_account": "SAVINGS",
  "type_of_action": "TRANSACTION",
  "bills_account_type": "NULL"
}

} }

2 个答案:

答案 0 :(得分:0)

我同意修复源更好,但即使格式正确,源仍然会有NULL(只是不作为字符串),因为你的问题也解决了删除它们我只会使用lodash或下划线来弹出淘汰不良信息。这是一个例子。你应该玩它来使它恰到好处。 Sails包含lodash作为依赖项,否则要在sails之外练习,只需确保包含lodash库并在必要时引用文档https://lodash.com/docs

_.mixin({
  cleanObject: function(o) {
    _.each(o, function(v, k) {
      if(k === 'NULL') {
        delete o[k];
      }
      else if(!v) {
        delete o[k];
      }
    });
    return o;
  }
});

var cleanedUp = _.cleanObject(JSONobj);

我可以理解,有时你会从源头获得不良信息。我不得不在我的时间内修补大量遗留系统。

答案 1 :(得分:0)

for(var key in JSONObj.regex_attributes.matching_attributes){
    for (var key2 in JSONObj.regex_attributes.matching_attributes[key])
    {
      var val = JSONObj.regex_attributes.matching_attributes[key][key2]
      if (val == 'NULL')
          delete JSONObj.regex_attributes.matching_attributes[key][key2]
    }
    }


  for (var key in JSONObj.regex_attributes.matching_attributes){
    var k=Object.keys(JSONObj.regex_attributes.matching_attributes[key]).length
    if (k==0){
      delete JSONObj.regex_attributes.matching_attributes[key]
    }
  }

  for (var key in JSONObj.regex_attributes){
    var k=Object.keys(JSONObj.regex_attributes[key]).length
    if (k==0){
      delete JSONObj.regex_attributes[key]
    }
  }

  var count2=0
  for (var key in JSONObj.regex_attributes.additional_processing[0]) {

      var val = JSONObj.regex_attributes.additional_processing[0][key];
      if (val == 'NULL'){
        delete JSONObj.regex_attributes.additional_processing[0][key]
        count2=count2+1
      }
  }

  if(count2==6){
      delete JSONObj.regex_attributes.additional_processing
  }

  var count3=0
  for (var key in JSONObj.regex_attributes.account_details) {

      var val = JSONObj.regex_attributes.account_details[key];
      if (val == 'NULL'){
        delete JSONObj.regex_attributes.account_details[key]
        count3=count3+1
      }

  }
  if (count3==5){
    delete JSONObj.regex_attributes.account_details
  }