在我的项目中,我想合并两个数组并根据唯一ID删除重复项,并将第二个数组中缺少的对象添加为swift中合并数组的子数组?
在这里,我给出了第一个NSArray:
[{
AccountNumber = 134679852;
AssessmentRollId = 12;
AssessmentYear = 1995;
City = houston;
DateCreated = "2018-01-22T18:43:53.6";
DateModified = "2018-01-22T18:43:53.603";
FIPS = 40125;
Id = 1223;
LandUseCode = 40125;
LotSizeSquareFeet = 45;
PropertyId = 3177;
},
{
AccountNumber = 134679852;
AssessmentRollId = 12;
AssessmentYear = 1995;
City = houston;
DateCreated = "2018-01-22T18:47:23.94";
DateModified = "2018-01-22T18:47:23.943";
FIPS = 40125;
Id = 1224;
LandUseCode = 40125;
LotSizeSquareFeet = 45;
PropertyId = 3177;
}]
这是第二个数组:
[{
AccountNumber = 547427;
City = "Berlin";
FIPS = 48453;
InspectionId = 143;
InspectionLogId = 90;
InspectionType = "Property Owner";
InspectionTypeId = 1;
ScheduledDate = "2018-01-22T00:00:00";
TemplateId = 1110;
TemplateName = "Beta validation Jennish";
UserId = 3177;
UserPropertyId = 1225;
},
{
AccountNumber = R157509;
City = "Santa Fe";
FIPS = 48167;
InspectionId = 144;
InspectionLogId = 91;
InspectionType = "Property Owner";
InspectionTypeId = 1;
ScheduledDate = "2018-01-22T00:00:00";
TemplateId = 1111;
TemplateName = "beta validation 3";
UserId = 3177;
UserPropertyId = 1226;
}]
请帮助我找到解决方案,我在技术知识方面有点弱,因为现在只有我在学习..
我想制作这种父类子格式:
[{
AccountNumber = 134679852;
AssessmentRollId = 12;
AssessmentYear = 1995;
City = houston;
DateCreated = "2018-01-22T18:43:53.6";
DateModified = "2018-01-22T18:43:53.603";
FIPS = 40125;
Id = 1223;
LandUseCode = 40125;
LotSizeSquareFeet = 45;
PropertyId = 3177;
InspectionDetails:{
InspectionId = 143;
InspectionLogId = 90;
InspectionType = "Property Owner";
InspectionTypeId = 1;
ScheduledDate = "2018-01-22T00:00:00";
TemplateId = 1110;
TemplateName = "Beta validation Jennish";
UserId = 3177;
UserPropertyId = 1225;
}
}]
这是我尝试使用Set ..
删除重复项let mergedArray:NSArray = Array1.addingObjects(from: Array2 as! [Any]) as NSArray
let mySet = NSSet(array : mergedArray as! [Any])
let uniquArray:NSArray = mySet.allObjects as NSArray
答案 0 :(得分:1)
首先,我希望你有一个字典数组(字符串:任意),并且它真的合并了数组中的字典是你想要的,如果我错了就纠正我。如果您没有要求拥有子数据,那么这很容易。你可以为字典创建一个扩展并循环遍历数组中的每个字典项并合并它们,因为事实并非如此,我提出了一个想法,我实现了你想要的,下面是代码(没有多少错误)处理,没有做太多的性能测试,我修改了你的测试数据,并将Accountnumber作为唯一的Id,并在第二个数组数据中进行了编辑,以便在数组之间存在唯一的id)
import UIKit
class ViewController: UIViewController {
var firstArray:[[String: Any]]!
var secondArray:[[String: Any]]!
override func viewDidLoad() {
super.viewDidLoad()
prepareData()
mergeDictionaries()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func prepareData(){
let acc1:[String: Any] = ["AccountNumber" :134679852,
"AssessmentRollId":12,
"AssessmentYear" :1995,
"City":"houston",
"DateCreated":"2018-01-22T18:43:53.6",
"DateModified":"2018-01-22T18:43:53.603",
"FIPS" :40125,
"Id":1223,
"LandUseCode":40125,
"LotSizeSquareFeet" :45,
"PropertyId" :3177]
let acc2:[String: Any] = ["AccountNumber" :134679854,
"AssessmentRollId":12,
"AssessmentYear" :1995,
"City":"houston",
"DateCreated":"2018-01-22T18:43:53.6",
"DateModified":"2018-01-22T18:43:53.603",
"FIPS" :40125,
"Id":1223,
"LandUseCode":40125,
"LotSizeSquareFeet" :45,
"PropertyId" :3177]
firstArray = [acc1, acc2]
/// Fill the second array
let acc3:[String: Any] = [ "AccountNumber" :134679852,
"City" :"Berlin",
"FIPS" :48453,
"InspectionId" :143,
"InspectionLogId" :90,
"InspectionType" :"Property Owner",
"InspectionTypeId" :1,
"ScheduledDate" :"2018-01-22T00:00:00",
"TemplateId" :1110,
"TemplateName" :"Beta validation Jennish",
"UserId":3177,
"UserPropertyId" :1225]
let acc4:[String: Any] = ["AccountNumber" :547427,
"City" :"Berlin",
"FIPS" :48453,
"InspectionId" :144,
"InspectionLogId" :91,
"InspectionType" :"Property Owner",
"InspectionTypeId" :1,
"ScheduledDate" :"2018-01-22T00:00:00",
"TemplateId" :1111,
"TemplateName" :"Beta validation Jennish",
"UserId":3178,
"UserPropertyId" :1226]
secondArray = [acc3, acc4]
}
// This method will loop through each dictionary data in the first array and find the corresponding dictionary in the second array and then find the difference of data as a new dictionary and then create a final merged disctionary with the new data as a new key, then this dictionary will be added to a new array which finally contains all the merged data
func mergeDictionaries(){
var mergedArray = [[String: Any]]()
for dict in firstArray{
var mergedDict = dict
let accNumber = mergedDict["AccountNumber"] as! Int
// Find the data from second array which is related to the one in the first array, assuming AccountNumber is unique
let filtered = secondArray.filter{ $0["AccountNumber"] as! Int == accNumber }
if filtered.count == 0{
continue
}
// Get the new data from the second dictionary
let missingData = mergedDict.diffData(dictionary: filtered[0])
print(missingData)
// Add the new data as a new dictionary to the merged dictionary
mergedDict.updateValue(missingData, forKey: "InspectionDetails")
print("Merged data is \(mergedDict)")
mergedArray.append(mergedDict)
}
}
}
extension Dictionary {
func diffData(
dictionary: Dictionary<Key, Value>)->[Key: Value] {
var dict = [Key: Value]()
for (key, value) in dictionary {
// Key already exists dont do anything, we want only new keys fro the second dictionary.
if ((self[key]) != nil){
continue
}
dict[key] = value
}
return dict
}
/// function to do simple merge of two dictionaries
mutating func merge(
dictionary: Dictionary<Key, Value>) {
for (key, value) in dictionary {
self[key] = value
}
}
}
合并的字典将包含以下数据,我相信这是你想要的,然后将这个字典添加到新的合并数组
["AccountNumber": 134679852,
"Id": 1223,
"InspectionDetails": ["InspectionType": "Property Owner",
"TemplateName": "Beta validation Jennish",
"UserId": 3177,
"TemplateId": 1110,
"InspectionId": 143,
"InspectionTypeId": 1,
"ScheduledDate": "2018-01-22T00:00:00",
"InspectionLogId": 90,
"UserPropertyId": 1225],
"City": "houston",
"AssessmentRollId": 12,
"LotSizeSquareFeet": 45,
"FIPS": 40125,
"DateModified": "2018-01-22T18:43:53.603",
"LandUseCode": 40125,
"DateCreated": "2018-01-22T18:43:53.6",
"PropertyId": 3177,
"AssessmentYear": 1995]