首页 > 解决方案 > 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 语法。

标签: javascriptpythonalgorithmdictionarycomparison

解决方案


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)


推荐阅读