javascript - Javascript比较对象中至少一对相同的对
问题描述
我有两个字典(JS 中的对象?),我想比较它们。
在while循环中,我想打印True
是否至少其中一对相同(即下面的字典),而不是整个字典相同(但如果整个字典相同,则语句必须仍然True
明显):
my_dict = {"Text1":"Text1", "Text2":"Text3", "text5":"text5"}
我知道在 python 中它会是这样的:
while any(key == value for key, value in my_dict.items()):
...
else:
...
但是我无法掌握正确的 JavaScript 语法。
解决方案
With Object.entries()
and some()
this is a one-liner:
let my_dict = {"Text1":"Text1", "Text2":"Text3", "text5":"text5"}
let answer = Object.entries(my_dict).some(([key,value]) => key == value);
console.log(answer)
推荐阅读
- ubuntu - opnsips start 给出“ERROR:core:main_loop: failed to fork module processes”并退出
- angular - 以角度更改焦点上的输入类型
- c# - 如何将随机int值获取到多次调用的预制件(不重复相同的值)?
- java - 连接到 Axon 服务器时更改节点的实例名称
- angular - ng build --prod 在 dist 文件夹中生成相同的文件
- architecture - 如何在单页中制作多个图表
- python - 是否可以使用 Python 将 .json 文件从 BigQuery 推送到 Firebase?
- botframework - Bot framework V4 中的同义词不再起作用
- mule - Mule 3 如何声明多个流变量
- amazon-cloudformation - AWS EKS 和 VPC 云形成