How to remove a value from dictionary with array of values. I have response from server like below:
[
"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
]
]
Second reponse
[
"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"
]
]
Both are array of dictionary, Both will have same key only. I know this structure is completely wrong.
business_proof only contains - 1, so i want to remove both places.
Here i need to remove key and values if anyone key values contains -1.
I am trying like this but it shows compiler error
finalValueArray.removeAll(where: { $0.contains(-1) })
You can use filter as far as I understand
var filteredItems = object.filter { !$0.value.contains(-1)}
You can get all elements which contain -1
var minusOneItems = object.filter { $0.value.contains(-1)}
and than
for negativeItem in minusOneItems {
object.removeValue(forKey: negativeItem.key)
}
It depends what you need.
You can simply use a combination of forEach and contains on the dictionary like so,
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]]
Or you can simply apply filter on dictionary like,
dictionary = dictionary.filter({ !$0.value.contains(-1) })
print(dictionary) //["reference_proof": [1, 2, 1]]
You can filter your dictionary to remove the entries that contains -1 in their value.
let filteredArrayOnDict = dataDict.filter { value.contains{ $0 != -1 } }
The filteredArrayOnDict is the array of tuples. Now if you want to create a dictionary from it. You can do this way:
let filteredDictionary = filteredArrayOnDict.reduce(into: [:]) { $0[$1.0] = $1.1}
Now you have the only the entries in filteredDictionary that doesn't have -1 in their value.
You can use while loop to remove all key-value pairs which contain -1 from both dictionaries.
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"]]
Create a struct and initialize the struct objects with both dictionary values. Then store the struct objects in an array. Now you can filter the array by its values
struct Proof {
var title: String
var arr: [Int]
var documents: [String]
}
var proofs = [Proof]()
dict1.forEach {
if let docs = dict2[$0.key] {
proofs.append(Proof(title: $0.key, arr: $0.value, documents: docs))
}
}
print(proofs)
let validProofs = proofs.filter { !$0.arr.contains(-1) }
print(validProofs)
Enumerate the index dictionary, look for occurrences of -1 and filter the indices. Then reverse the found indices and remove the items in both arrays. The code considers that both dictionaries are value types
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)
}
}
I have been messing around with the .sort() and .concat() functions trying to combine multiple arrays of numbers and text into one and chronologically order it. It seems to work fine if only numbers are input into the array but as soon as there is text it seems to place it randomly, in this case between 6 and 7. Also, if I remove the number 3 from the array, the number 4 ends up out of order after the text, both being between 9 and 10. I have no idea why it is doing this if anyone could help.
With only numbers:
var multiArray = [[2, 8, 1, 7, 9], [6, 3, 5, 4, 10]];
// returns 1,10,2,3,4,5,6,7,8,9
document.write(multiArray[0]
.concat(multiArray[1])
.sort()
+"<br/>");
// returns 1,2,3,4,5,6,7,8,9,10
document.write(multiArray[0]
.concat(multiArray[1])
.sort((a,b)=>a-b)
+"<br/>");
and with text
var multiArray = [[2, 8, 1, 7, 9], [6, 3, 5, "text", 4, 10]];
// returns 1,2,3,4,5,6,text,7,8,9,10
document.write(multiArray[0]
.concat(multiArray[1])
.sort((a,b)=>a-b)
+"<br/>");
and without 3
var multiArray = [[2, 8, 1, 7, 9], [6, 5, "text", 4, 10]];
// returns 1,2,5,6,7,8,9,text,4,10
document.write(multiArray[0]
.concat(multiArray[1])
.sort((a,b)=>a-b)
+"<br/>");
first, combine all arrays in a single array
var multiArray = [[2, 8, 1, 'foo', 7, '1', 9], [6, 5, 'text', 4, 'bar', 10]];
var flattenArray = [].concat(...multiArray); // [ 2, 8, 1, 'foo', 7, '1', 9, 6, 5, 'text', 4, 'bar', 10 ]
then sort separately numbers and strings
const sortedNumbers = flattenArray
.filter(n => typeof n === 'number')
.sort((a, b) => a - b);
const sortedStrings = flattenArray
.filter(n => typeof n === 'string')
.sort();
finally, concat both
const sorted = sortedNumbers.concat(sortedStrings); // [ 1, 2, 4, 5, 6, 7, 8, 9, 10, '1', 'bar', 'foo', 'text' ]
I assume you want strings after your numbers. Otherwise, simply invert the concatenation:
const sorted = sortedStrings.concat(sortedNumbers);
My ultimate goal is to get an array of Ints extracted from a String inputted in a textfield. For this I created a function whose parameter is a String. I use a for-loop to get all the characters from the textfield into an array. Then I loop through the array and append only the numbers (if they're <= 24) to a new array. When I print the array with just numbers, I get the first element of array1 repeating. Why is this happening?
var array1 = [String]()
var array2 = [Int]()
func getDigits (userInput: String) {
for element in userInput {
array1.append(String(element))
}
var mx = 0
var fx = 1
func findNumbers() {
for number in 1...24 {
if array1[mx] == "\(fx)" {
array2.append(Int(String(array1[mx]))!)
} else {
fx += 1
findNumbers()
}
}
}
findNumbers()
print (array2)
}
getDigits(userInput: "4-5")
Output: [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
This will support multi-digit numbers in your input string, and also filter out-of-range values.
import Foundation
func parseNumbers (userInput: String, splitBy: String, min: Int, max: Int) -> Array<Int> {
let separators = CharacterSet(charactersIn: splitBy)
let numericStrings = userInput.components(separatedBy: separators)
var numArray = [Int]()
for item in numericStrings {
if let number = Int(item) {
if (number >= min) && (number <= max) {
numArray.append(number)
}
}
}
return numArray
}
print(parseNumbers(userInput: "5:hello,34-7 12;103,abc", splitBy: " -:,;", min:1, max:24))
output: [5, 7, 12]
try this
func getDigits (userInput: String) {
let splitsCharacters = ",;:- "
let seperators = CharacterSet(charactersIn: splitsCharacters)
let updateInput = userInput.components(separatedBy: seperators)
print(array1)
func findNumbers() {
for item in updateInput {
if let number = Int(item) {
if number <= 24{
array2.append(number)
}
}
}
}
findNumbers()
print (array2)
}
getDigits(userInput: "4-5 hello, 34;12,23")
Output : [4, 5,12,23]
I am not sure what you are trying to do but you can try something like
Swift 4
func getDigits (userInput: String) {
var resultArray = [Int]()
for char in userInput {
if let number = Int("\(char)"), number < 24 {
resultArray.append(number)
}
}
print(resultArray)
}
testing
getDigits(userInput: "4-5")
//prints => [4, 5]
You need just 1 array to hold the numbers
Note: in swift for the string is array type so you can loop on it directly, but for earlier versions you can use:
for char in userInput.characters {
}
to get the array of string characters