javascript - How do I query a Key-Value object in Java
问题描述
If I have an array of strings, how do I query all VALUES in a key-value object with my listOfStrings, and return the related KEYS??
"listOfStrings": ["4444", "5555"]
"secretCodes": [
{
"secret": "John",
"value": "4444"
},
{
"secret": "Paul",
"value": "0001"
},
{
"secret": "George",
"value": "0002"
},
{
"secret": "Ringo",
"value": "5555"
},
{
"secret": "Pete",
"value": "0008"
}
]
解决方案
If you want to return the secret names that correspond to the values in the codes array you could do this:
let secretCodes = [
{ secret: "John", value: "4444" },
{ secret: "Paul", value: "0001" },
{ secret: "George", value: "0002" },
{ secret: "Ringo", value: "5555" },
{ secret: "Pete", value: "0008" },
];
function getSecretName(codes) {
let matchingValues = secretCodes.filter(obj => codes.includes(obj.value));
return matchingValues.map(value => value.secret);
}
console.log(getSecretName(["4444", "5555"]));
// returns ["John", "Ringo"]
推荐阅读
- project-reactor - 如何以反应器方式编写快捷循环
- oracle-cloud-infrastructure - 引导卷和块卷有什么区别?
- json - 有没有办法利用 JSON 路径来填写搜索字段并以超链接的形式返回搜索结果?
- c# - 如何获取MEF中的所有方法
- react-native - COMPONENT 已从 react-native 核心中提取,并将在未来的版本中删除。现在可以从新位置安装和导入它
- c++ - 在 C++ 中获取多行句子输入
- c - 启用配置文件引导优化时,OpenMP 并行元素增量得到错误结果
- javascript - 旋转设备时重新加载特定的 div
- azure - Azure 函数可见性超时
- c++ - 在游戏中添加计时器