arrays - 如何在scala中过滤掉2个不同地图的公共键
问题描述
我想检索一个列表/数组(假设A)的键值,该键存在于另一个列表/数组中(假设B)
val B: List[String] = List("key1","key3") //I can refactor the type if needed
val paramNames: Array[String] = parameterNames // ["key1", "key2", "key3"]
val paramValues: Array[AnyRef] = args // [1, "value", Obj]
val A: Array[(String,AnyRef)] = paramNames.zip(paramValues) // [("key1", 1), ("key2", "value"), ("key3", Obj)]
//now I want to retrieve from A, all keys exist in B with their values
//to get [("key1", 1), ("key3", Obj)]
解决方案
只需使用过滤器:
val C = A.filter(k => B.contains(k._1))
这将只获得元组,其键包含在B
.
推荐阅读
- javascript - HTML 检查是否发送了数据,而不是使用 onclick
- spring-boot - 来自@kafkaListener 的日志主题名称
- javascript - 如何将大文件分块并上传到 Google 存储桶
- node.js - 如果在循环中,为什么广播到特定 ID 将不起作用
- javascript - 当 args 是具有可选 args 的函数时,如何推断函数重载 TS 类型
- sql-server - dbatools(或 SQL Server 的 SMO)- 模式导出期间的表排序
- angular - 对象的访问属性
- javascript - 我的 Phaser 3 游戏仅加载 1 个文本链的字体。我不知道为什么
- oracle - Oracle RECORDS 和记录类型表
- css - CSS图像背景未显示在某些移动页面上