A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Set/isDisjointFrom below:

Set.prototype.isDisjointFrom() - JavaScript | MDN

Set.prototype.isDisjointFrom()

Baseline 2024

Newly available

Set 实例的 isDisjointFrom() 方法接受一个集合并返回一个布尔值来指示当前集合与给定集合是否不存在公共元素。

语法 参数
other

一个 Set 或类集合对象。

返回值

如果当前集合与 other 集合没有公共元素,则返回 true;否则返回 false。

描述

如果两个集合没有公共元素,那么它们是不相交的。使用数学记号:

A  ä¸Ž  B  ä¸ç›¸äº¤  ⇔ A ∩ B = ∅ A\text{ 与 }B \text{ 不相交 } \Leftrightarrow A\cap B = \empty

使用维恩图表示:

isDisjointFrom() 接受类集合对象作为 other 参数。方法要求 this 是一个 Set 的实例,因为它不调用任何用户代码而直接获取 this 中存储的数据。然后,方法的行为取决于 this 和 other 的元素数量:

由于这种实现,isDisjointFrom() 的效率主要取决于 this 和 other 中数量较少的集合(假定两个集合都能以次线性时间复杂度访问)。

示例 使用 isDisjointFrom()

小于 20 的完全平方数集和小于 20 的质数集是不相交的,因为按定义完全平方数可分解为两个整数的乘积,而且 1 不是质数:

const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);
const squares = new Set([1, 4, 9, 16]);
console.log(primes.isDisjointFrom(squares)); // true

而小于 20 的完全平方数集和小于 20 的合数集是相交的,因为按定义除 1 以外的完全平方数都是合数:

const composites = new Set([4, 6, 8, 9, 10, 12, 14, 15, 16, 18]);
const squares = new Set([1, 4, 9, 16]);
console.log(composites.isDisjointFrom(squares)); // false
规范 浏览器兼容性 参见

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4