删除[String:[NSNumber]]中匹配号码的键

时间:2019-06-27 05:49:55

标签: arrays swift higher-order-functions

如何从具有值数组的字典中删除值。我收到来自服务器的响应,如下所示:

[  
   "business_proof":[  
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      -1,// business_proof contains -1, I want to remove this key like wise any other contains
      0,
      0
   ],
   "reference_proof":[  
      1,
      2,
      1
   ],
   "vehicle_proof":[  
      1,
      1,
      2
   ],
   "previous_loan_track":[  
      2,
      2,
      0,
      0,
      2,
      2
   ],
   "banking_proof":[  
      1,
      1
   ],
   "income_proof":[  
      0,
      0,
      2,
      0,
      2,
      1,
      2,
      0,
      0
   ],
   "signature_proof":[  
      2,
      2,
      1,
      2,
      2,
      2
   ],
   "employment_proof":[  
      2,
      1,
      2,
      2,
      2,
      2,
      2
   ],
   "guarantor_proof":[  
      1,
      2,
      2
   ],
   "pdc_proof":[  
      1,
      0
   ],
   "address_proof":[  
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      3
   ],
   "age_proof":[  
      2,
      2,
      2,
      2,
      2,
      2,
      1,
      2
   ],
   "contact_proof":[  
      0,
      2,
      2
   ],
   "photo_id_proof":[  
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2
   ]
]

第二响应

[  
   "signature_proof":[  
      "pan_card",
      "driving_licence",
      "accepted_documents",
      "passport",
      "cancelled_cheque",
      "bank_report"
   ],
   "guarantor_proof":[  
      "accepted_documents",
      "co_applicant",
      "guarantor"
   ],
   "previous_loan_track":[  
      "housing_loan",
      "vehicle_loan",
      "over_draft_limit",
      "accepted_documents",
      "business_loan",
      "personal_loan"
   ],
   "address_proof":[  
      "bank_statement",
      "voter_id",
      "rental_agreement",
      "eb_bill",
      "registration_document",
      "hr_letter",
      "driving_licence",
      "property_tax_receipt",
      "telephone_bill",
      "cc_statement",
      "gas_bill",
      "aadhaar_card",
      "passport",
      "ration_card",
      "accepted_documents"
   ],
   "vehicle_proof":[  
      "vehi_insurance",
      "vehi_rc",
      "accepted_documents"
   ],
   "business_proof":[  
      "business_commencement_certificate",
      "ssi_msme_certificate",
      "business_transactions",
      "mou",
      "aoa",
      "gst_no",
      "tan_no",
      "business_agreements",
      "accepted_documents",
      "shop_and_establishment_certificate",
      "incorporation_certificate"
   ],
   "banking_proof":[  
      "bank_statement",
      "accepted_documents"
   ],
   "income_proof":[  
      "form_16",
      "profit_loss_statement",
      "rental_income_proof",
      "payslip",
      "income_in_cash_proof",
      "accepted_documents",
      "brokerage_income_proof",
      "it_returns",
      "audited_balance_sheet"
   ],
   "reference_proof":[  
      "ref2",
      "accepted_documents",
      "ref1"
   ],
   "employment_proof":[  
      "employee_id_card",
      "accepted_documents",
      "payslip",
      "relieving_letter",
      "comp_app_letter",
      "hr_letter",
      "epf_no_uan_no"
   ],
   "age_proof":[  
      "employee_id_card",
      "ration_card",
      "pan_card",
      "passport",
      "voter_id",
      "school_certificate",
      "accepted_documents",
      "aadhaar_card"
   ],
   "contact_proof":[  
      "accepted_documents",
      "landline_bill",
      "mobile_bill"
   ],
   "photo_id_proof":[  
      "employee_id_card",
      "nrega_card",
      "ration_card",
      "bank_passbook",
      "pan_card",
      "passport",
      "voter_id",
      "driving_licence",
      "accepted_documents",
      "aadhaar_card"
   ],
   "pdc_proof":[  
      "cheque_book",
      "accepted_documents"
   ]
]

两者都是字典数组,两者都只有相同的键。我知道这种结构是完全错误的。

business_proof 仅包含-1,因此我想删除两个位置。

如果有人的键值包含-1,我需要在这里删除键和值。

我正在尝试这样,但是它显示了编译器错误

finalValueArray.removeAll(where: { $0.contains(-1) })

5 个答案:

答案 0 :(得分:1)

据我所知,您可以使用filter

var filteredItems = object.filter { !$0.value.contains(-1)}

您可以获得包含-1的所有元素

var minusOneItems = object.filter { $0.value.contains(-1)}

for negativeItem in minusOneItems {
    object.removeValue(forKey: negativeItem.key)
}

这取决于您的需求。

答案 1 :(得分:1)

您可以像这样简单地在forEach上使用 contains dictionary 的组合,

var dictionary = ["business_proof": [0, 0, 1, -1, 2, -1], "reference_proof": [1, 2, 1], "vehicle_proof": [-1, 0, 0, 2]]

dictionary.forEach { (key,value) in
    dictionary[key] = value.contains(-1) ? nil : value
}

print(dictionary) //["reference_proof": [1, 2, 1]]

或者您也可以在filter上应用 dictionary

dictionary = dictionary.filter({ !$0.value.contains(-1) })

print(dictionary) //["reference_proof": [1, 2, 1]]

答案 2 :(得分:1)

您可以使用while循环从两个字典中删除所有包含key-value的{​​{1}}对。

-1


创建一个var dict1 = ["business_proof":[0,0,0,0,0,0,0,0,-1,0,0],"reference_proof":[1,2,1],"vehicle_proof":[1,1,2],"previous_loan_track":[2,2,0,0,2,2],"banking_proof":[1,1],"income_proof":[0,0,2,0,2,1,2,0,0],"signature_proof":[2,2,1,2,2,2],"employment_proof":[2,1,2,2,2,2,2],"guarantor_proof":[1,2,2],"pdc_proof":[1,0],"address_proof":[2,2,2,2,2,2,2,2,2,2,2,2,2,2,3],"age_proof":[2,2,2,2,2,2,1,2],"contact_proof":[0,2,2],"photo_id_proof":[2,2,2,2,2,2,2,2,2,2]] var dict2 = ["signature_proof":["pan_card","driving_licence","accepted_documents","passport","cancelled_cheque","bank_report"],"guarantor_proof":["accepted_documents","co_applicant","guarantor"],"previous_loan_track":["housing_loan","vehicle_loan","over_draft_limit","accepted_documents","business_loan","personal_loan"],"address_proof":["bank_statement","voter_id","rental_agreement","eb_bill","registration_document","hr_letter","driving_licence","property_tax_receipt","telephone_bill","cc_statement","gas_bill","aadhaar_card","passport","ration_card","accepted_documents"],"vehicle_proof":["vehi_insurance","vehi_rc","accepted_documents"],"business_proof":["business_commencement_certificate","ssi_msme_certificate","business_transactions","mou","aoa","gst_no","tan_no","business_agreements","accepted_documents","shop_and_establishment_certificate","incorporation_certificate"],"banking_proof":["bank_statement","accepted_documents"],"income_proof":["form_16","profit_loss_statement","rental_income_proof","payslip","income_in_cash_proof","accepted_documents","brokerage_income_proof","it_returns","audited_balance_sheet"],"reference_proof":["ref2","accepted_documents","ref1"],"employment_proof":["employee_id_card","accepted_documents","payslip","relieving_letter","comp_app_letter","hr_letter","epf_no_uan_no"],"age_proof":["employee_id_card","ration_card","pan_card","passport","voter_id","school_certificate","accepted_documents","aadhaar_card"],"contact_proof":["accepted_documents","landline_bill","mobile_bill"],"photo_id_proof":["employee_id_card","nrega_card","ration_card","bank_passbook","pan_card","passport","voter_id","driving_licence","accepted_documents","aadhaar_card"],"pdc_proof":["cheque_book","accepted_documents"]] while let invalid = dict1.first(where: { $0.value.contains(-1) }) { dict1.removeValue(forKey: invalid.key) dict2.removeValue(forKey: invalid.key) } print(dict1)//["reference_proof":[1,2,1],"vehicle_proof":[1,1,2],"previous_loan_track":[2,2,0,0,2,2],"banking_proof":[1,1],"income_proof":[0,0,2,0,2,1,2,0,0],"signature_proof":[2,2,1,2,2,2],"employment_proof":[2,1,2,2,2,2,2],"guarantor_proof":[1,2,2],"pdc_proof":[1,0],"address_proof":[2,2,2,2,2,2,2,2,2,2,2,2,2,2,3],"age_proof":[2,2,2,2,2,2,1,2],"contact_proof":[0,2,2],"photo_id_proof":[2,2,2,2,2,2,2,2,2,2]] print(dict2)//["signature_proof":["pan_card","driving_licence","accepted_documents","passport","cancelled_cheque","bank_report"],"guarantor_proof":["accepted_documents","co_applicant","guarantor"],"previous_loan_track":["housing_loan","vehicle_loan","over_draft_limit","accepted_documents","business_loan","personal_loan"],"address_proof":["bank_statement","voter_id","rental_agreement","eb_bill","registration_document","hr_letter","driving_licence","property_tax_receipt","telephone_bill","cc_statement","gas_bill","aadhaar_card","passport","ration_card","accepted_documents"],"vehicle_proof":["vehi_insurance","vehi_rc","accepted_documents"],"banking_proof":["bank_statement","accepted_documents"],"income_proof":["form_16","profit_loss_statement","rental_income_proof","payslip","income_in_cash_proof","accepted_documents","brokerage_income_proof","it_returns","audited_balance_sheet"],"reference_proof":["ref2","accepted_documents","ref1"],"employment_proof":["employee_id_card","accepted_documents","payslip","relieving_letter","comp_app_letter","hr_letter","epf_no_uan_no"],"age_proof":["employee_id_card","ration_card","pan_card","passport","voter_id","school_certificate","accepted_documents","aadhaar_card"],"contact_proof":["accepted_documents","landline_bill","mobile_bill"],"photo_id_proof":["employee_id_card","nrega_card","ration_card","bank_passbook","pan_card","passport","voter_id","driving_licence","accepted_documents","aadhaar_card"],"pdc_proof":["cheque_book","accepted_documents"]] 并使用两个字典值初始化结构对象。然后将结构对象存储在数组中。现在您可以按其值过滤数组

struct

答案 3 :(得分:1)

您可以过滤字典以删除其值中包含-1的条目。

let filteredArrayOnDict = dataDict.filter {  value.contains{ $0 != -1 } }

filteredArrayOnDict 是元组的数组。现在,如果您要从中创建字典。您可以这样做:

let filteredDictionary = filteredArrayOnDict.reduce(into: [:]) { $0[$1.0] = $1.1}

现在,您只有filterDictionary中值不为-1的条目。

答案 4 :(得分:1)

枚举索引字典,查找出现-1并过滤indices。然后反转找到的索引并删除两个数组中的项目。代码认为这两个字典都是值类型

var indexDict = ["business_proof":[0,0,0,0,0,0,0,0,-1,0,0] ...
var valueDict = ["signature_proof":["pan_card","driving_licence","accepted_documents","passport","cancelled_cheque","bank_report"] ...


for (key, value) in indexDict {
    let foundIndices = value.indices.filter({value[$0] == -1})
    for index in foundIndices.reversed() {
        indexDict[key]!.remove(at: index)
        valueDict[key]!.remove(at: index)
    }

}