- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 2 个数组从 2 个不同的 API 接收
{
addrLine1 = "Al Thanyah Fifth, Dubai, United Arab Emirates";
addrLine2 = " ";
apntDate = "14 Feb 2018";
apntDt = "2018-02-14 15:31:30";
apntTime = "03:31 pm";
apptLat = "25.071102142334";
apptLong = "55.142993927002";
"appt_duration" = "";
bid = 555;
bookType = 1;
cancelAmt = 30;
"cat_id" = 591da979227951a10f004ad7;
"cat_name" = "Car Wash";
cid = 21;
"coupon_discount" = 0;
"customer_notes" = "";
"distance_met" = 120;
email = "fawasfais@gmail.com";
expireTime = "";
fname = Fawas;
"job_imgs" = 0;
"job_start_time" = "1970-01-01 00:00:00";
"job_timer" = "";
pPic = "https://s3.amazonaws.com/iserve/ProfileImages/20170720110011AM.png";
"payment_type" = 1;
phone = 568573570;
"price_per_min" = 10;
"pro_notes" = "";
services = "5923f21c2279518a1661cb09,5923f22d2279513f1861cb09";
status = 2;
statusMsg = "Provider Accepted.";
timer = "";
"timer_status" = "";
"visit_amount" = 60;
}
{
"price_per_unit" = 30;
"sub_cat_id" = 5923f21c2279518a1661cb09;
"sub_cat_name" = SUV;
unit = 1;
},
{
"price_per_unit" = 45;
"sub_cat_id" = 5923f22d2279513f1861cb09;
"sub_cat_name" = Sedan;
unit = 1;
}
{
"price_per_unit" = 65;
"sub_cat_id" = 5923f23e2279518a1661cb09;
"sub_cat_name" = Bus;
unit = 1;
},
{
"price_per_unit" = 75;
"sub_cat_id" = 5923f24f2279513f1861cb09;
"sub_cat_name" = Lorry;
unit = 1;
}
我想将上述数组与第一个数组中的“services”键和第二个数组中的“sub_cat_id”进行比较,最后打印包含车辆的公共(public)服务 ID 的 price_per_unit。
示例 - 在第一个数组中找到 5923f21c2279518a1661cb09,5923f22d2279513f1861cb09 id,因此打印第二个数组中两个 id 的 price_per_units - 30、45
有什么建议吗? (如果任何代码片段需要回答评论中提到的这个问题,我将使用所需的代码编辑和更新问题。
This is how I get the second array from the server
-(void)getServiceTypes
{
NSDictionary *queryParams;
queryParams = @{
@"ent_sess_token":flStrForStr([[NSUserDefaults standardUserDefaults] objectForKey:KDAcheckUserSessionToken]),
@"ent_dev_id":flStrForStr([[NSUserDefaults standardUserDefaults] objectForKey:kPMDDeviceIdKey]),
@"ent_catid":_dictAppointmentDetails[@"cat_id"]
};
NetworkHandler *handler = [NetworkHandler sharedInstance];
[handler composeRequestWithMethod:@"getSubCategory"
paramas:queryParams
onComplition:^(BOOL succeeded, NSDictionary *response) {
if (succeeded) {
[self getServiceTypesResponse:response];
}
}];
NSLog(@"param%@",queryParams);
}
-(void)getServiceTypesResponse:(NSDictionary *)response
{
NSLog(@"GETSERVICETYPES%@",response);
serviceTypeDict = response[@"data"];
NSLog(@"SERVICETYPEDIC%@",serviceTypeDict);
serviceTypeArray = response[@"data"];
NSLog(@"SERVICETYPEARRAY%@",serviceTypeArray);
serviceTypeIDArray = [[serviceTypeArray valueForKey:@"sub_cat_id"]copy];
NSLog(@"SERVICETYPEIDARRAY%@",serviceTypeIDArray);
}
谢谢
最佳答案
让它在 Playground 上工作:
let booking: [String: Any] = [
"services": "5923f21c2279518a1661cb09,5923f22d2279513f1861cb09",
"status":2
]
let selectedServices: [[String: Any]] = [
[
"price_per_unit": 30,
"sub_cat_id": "5923f21c2279518a1661cb09",
"sub_cat_name": "SUV",
"unit": 1
],
[
"price_per_unit": 45,
"sub_cat_id": "5923f22d2279513f1861cb09",
"sub_cat_name": "Sedan",
"unit": 1
],
[
"price_per_unit": 65,
"sub_cat_id": "5923f23e2279518a1661cb09",
"sub_cat_name": "Bus",
"unit": 1
],
[
"price_per_unit": 75,
"sub_cat_id": "5923f24f2279513f1861cb09",
"sub_cat_name": "Lorry",
"unit": 1
]
]
if let bookingServices = booking["services"] as? String {
let services: [String] = bookingServices.components(separatedBy: ",")
let commonServices = selectedServices.filter({ services.contains( $0["sub_cat_id"] as? String ?? "" ) })
print(commonServices)
}
等效于 Objective-C:
NSDictionary *booking = @{
@"services": @"5923f21c2279518a1661cb09,5923f22d2279513f1861cb09",
@"status": @2
};
NSArray *selectedServices = @[
@{
@"price_per_unit": @30,
@"sub_cat_id": @"5923f21c2279518a1661cb09",
@"sub_cat_name": @"SUV",
@"unit": @1
},
@{
@"price_per_unit": @45,
@"sub_cat_id": @"5923f22d2279513f1861cb09",
@"sub_cat_name": @"Sedan",
@"unit": @1
},
@{
@"price_per_unit": @65,
@"sub_cat_id": @"5923f23e2279518a1661cb09",
@"sub_cat_name": @"Bus",
@"unit": @1
},
@{
@"price_per_unit": @75,
@"sub_cat_id": @"5923f24f2279513f1861cb09",
@"sub_cat_name": @"Lorry",
@"unit": @1
}
];
NSString *servicesString = [booking valueForKey: @"services"];
NSArray *bookingServices = [servicesString componentsSeparatedByString:@","];
NSMutableArray *commonServices = [[NSMutableArray alloc] init];
for (NSDictionary *selectedService in selectedServices) {
NSString *sub_cat_id = selectedService[@"sub_cat_id"];
if([bookingServices containsObject: sub_cat_id]) {
[commonServices addObject: selectedService];
}
}
NSLog(@"%@", commonServices);
关于ios - 如何从两个不同的数组中找到共同的对象并打印另一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48787375/
所以,我有一个类似于 this one 的用例,但我觉得有一些额外的细节值得提出一个新问题。 ( related questions ,供引用) 我正在编写一个实现 a cycle 的数据结构.基本设
我正在使用 Django 编写一个社交网络应用程序,需要实现类似于 Facebook“Mutual Friends”概念的功能。我有一个像这样的简单模型: class Friend(models.Mo
我有一个 iOS 应用程序,用户可以在其中使用 Facebook 登录并授予 user_friends 权限。从 Graph API 2.0 开始,Facebook 声称你无法获取两个人之间所有的共同
我想知道将来对我来说最简单的方法是什么,可以使查询既有效又不那么复杂。 我应该像这样保存双向关系吗 from_id=1, to_id=2from_id=2, to_id=1 或者只创建一个唯一的行 f
我是一名优秀的程序员,十分优秀!