空调不热是什么原因| 惠字五行属什么| 6s是什么| 先兆性流产是什么意思| 雪纳瑞什么颜色最贵| 头发厚适合剪什么发型| 小孩晚上睡觉流口水是什么原因| 检查胸部应该挂什么科| 嘴上有痣代表什么| 10度左右穿什么衣服合适| 梅菌是什么病| 男人为什么会得尿结石| 白带有血丝是什么原因| 宫颈纳氏囊肿是什么| 笔画最多的字是什么| 肝气不舒有什么症状| 小鸟为什么会飞| 踏实是什么意思| 僵尸肉吃了有什么危害| 人体最大的器官是什么| 出道是什么意思| 猫驱虫药什么牌子好| 指甲有横纹是什么原因| 甲醛中毒吃什么药| 榧读什么| 贬低是什么意思| 18年属什么生肖| 前列腺炎吃什么中成药| 晒伤了涂什么药| tat是什么意思| 明知故犯的故是什么意思| 路上行人匆匆过是什么歌| 综合用地是什么性质| 五点多是什么时辰| 湿疹和荨麻疹有什么区别| 日本兵为什么不怕死| 比熊吃什么牌子狗粮好| 什么叫心悸| 胃息肉吃什么药治疗| 富贵包挂什么科| 叛逆是什么意思| 外阴炎吃什么药| 封豕长蛇是什么意思| 领袖是什么意思| 中药不能和什么一起吃| 脚踝后面的筋疼因为什么| 土是什么生肖| 肉苁蓉与什么搭配好| 稀饭配什么菜好吃| 无名指长痣代表什么| 宝宝大便发白是什么原因| 中规中矩是什么意思| 凝胶是什么| 长得什么| 贞洁是什么意思| 总是抽筋是什么原因| 什么鸟一生只有一个伴侣| 办理社保卡需要什么资料| 裂孔疝是什么病| 晚上八点到九点是什么时辰| 幽灵是什么意思| 什么食物补钙效果最好最快| 俄罗斯什么东西值得买| 赤脚走路有什么好处| 2月19日什么星座| 股骨头疼痛吃什么药| 家里为什么会有蚂蚁| 咳血鲜红是什么病呢| 牛肉用什么腌制比较嫩| 什么是一二三级医院| 弱酸性是什么意思| 菱角什么时候上市| 山药和淮山有什么区别| amk是什么品牌| 经期不能吃什么| 头孢有什么用| 产后大出血一般发生在什么时候| 疽是什么意思| miko是什么意思| 脑筋急转弯什么东西越洗越脏| 金利来属于什么档次| 尿毒清颗粒主治什么病| 看日历是什么生肖| 粉红粉红的什么填空| 脓血症是什么病严重吗| 岳云鹏为什么这么火| 胸椎退行性变什么意思| 查输卵管通不通做什么检查| 翻什么越什么| 忧心忡忡是什么意思| 骨转移用什么药能治愈| 外向孤独症是什么意思| 七情六欲是什么意思| 梦见挖土豆是什么意思| 宋朝之前是什么朝代| 过氧化氢阳性什么意思| 大姨夫是什么| 人民币用什么材料做的| 右手中指指尖麻木是什么原因| 眼力见是什么意思| sayno是什么意思| 停车坐爱枫林晚中的坐是什么意思| 没品什么意思| 脑梗是什么原因引起的| 为什么喜欢秋天| 大使是什么行政级别| 二院是什么医院| 男性睾丸一边大一边小是什么原因| 硫磺是什么东西| 吃避孕药为什么要吃维生素c| 无花果吃多了有什么坏处| 分析是什么意思| 聚乙二醇是什么东西| 化疗后白细胞低吃什么补得快| 肾阴虚火旺吃什么药| 猴子怕什么| 吃什么可以生发| 女性睾酮低说明什么| hpv45型阳性是什么意思| soho是什么意思| 三焦不通吃什么中成药| 肺肿物是什么意思| 老虎的祖先是什么动物| 总是打嗝是什么原因| 痔疮应该挂什么科室| 胆挂什么科| 海米是什么| 腰间盘膨出是什么意思| 虚有其表的意思是什么| 纳纹女装属于什么档次| cc什么意思| 脸上爱出汗是什么原因| 囊腺瘤是什么| 大姨妈是什么意思| 大耳读什么| 人为什么会低血糖| 四爱是什么| hcc是什么意思| 二月初十是什么星座| 刘五行属性是什么| 89年五行属什么| 血热皮肤瘙痒吃什么药| 芝兰是什么意思| 老年人嘴唇发紫是什么原因| IOM是什么意思| 契合是什么意思| guess是什么品牌| 静脉穿刺是什么意思| 独白什么意思| as材质是什么材料| 金丝皇菊有什么功效| 声音的高低叫什么| 类风湿关节炎吃什么好| 僵尸为什么怕糯米| 荨麻疹吃什么食物好| 半夏是什么| 牙冠是什么意思| ot是什么| 乳清蛋白是什么| 牛奶什么时间喝最好| 什么是糙米| 放线是什么意思| 石榴叶子泡水喝有什么功效| 身份证穿什么颜色的衣服| 人属于什么界门纲目科属种| 361是什么意思| 肺癌晚期有什么症状| 走路带风是什么意思| 感冒流鼻涕咳嗽吃什么药好| 痤疮是由什么引起的| 621什么星座| 黑五是什么时候| 气虚是什么意思| 一个火一个斤念什么| 多动症是什么原因造成| 中医康复技术学什么| 艾滋病会有什么症状| 胆结石属于什么科| 羊病是什么病| cta是什么| 大生化能查出什么病来| 豌豆有什么营养价值| 万里长城是什么生肖| 什么病需要化疗| 神经元特异性烯醇化酶是什么意思| 卵巢增大是什么原因引起的| 12年属什么生肖| 处女膜破了有什么影响| 甜杆和甘蔗有什么区别| 用纸可以折什么| 六个口是什么字| 老年痴呆吃什么药| 锁阳是什么| 为什么北极没有企鹅| 忽冷忽热是什么意思| 长沙有什么区| 肾结石要注意些什么| 老放屁是什么病的征兆| 做核磁共振挂什么科| 菲妮迪女装是什么档次| 孔子名什么| 哦多桑是什么意思| 为什么用| 胆囊炎吃什么食物好| 土豆可以做什么美食| 农历五月十八是什么星座| 例假来的是黑色的是什么原因| 黄精什么人不能吃| 吃饭恶心想吐是什么原因| 平均血小板体积低是什么原因| mr什么意思| inr是什么意思| 花五行属什么| 4月1日是什么星座| 嗓子疼有痰吃什么药| 脑震荡挂什么科| 洋葱什么时候种| 风餐露宿是什么生肖| 1953年属蛇的是什么命| 男生回复嗯嗯代表什么| 留低是什么意思| 手足口病有什么症状| 10月5号什么星座| 刷墙的白色涂料叫什么| 清宫和人流有什么区别| 什么花走着开| 粉刺用什么药膏最有效| 茶色尿是什么原因引起的| 蚊子喜欢叮什么样的人| 凯字五行属什么| 早上口干口苦是什么原因| 奋笔疾书的疾是什么意思| 单立人加吉念什么| 活珠子是什么| 浅粉色配什么颜色好看| 尼泊尔属于什么国家| 做眉毛有什么危害| 养肝吃什么| 太平间是什么意思| c14阳性 是什么意思| 术前四项检查是什么| 胆管炎吃什么药| 检测怀孕最准确的方法是什么| 青云志3什么时候上映| 婴儿便秘怎么办什么方法最有效| 公务员是什么职业| 高血压饮食上注意什么| 姓名字号是什么意思| 直博是什么意思| 为什么外阴老是长疖子| 鸡蛋可以炒什么菜| 退位让贤是什么意思| 堪忧是什么意思| 韩世忠为什么不救岳飞| 贪小失大什么意思| 幽门螺杆菌吃什么药好| 眼压高是什么症状| 手臂肌肉跳动是什么原因| 躁郁症是什么| 路人甲是什么意思| 痔疮是什么样的| 英国全称是什么| 面色潮红是什么原因| 三撇读什么| 百度
New JavaScript Set methods title. A vibrant gradient with a JavaScript logo in the bottom-left corner and a venn diagram in the top-right corner.

聊城农业提质的关键之举

Author avatarBrian Smith5 minute read
百度 机关党委送上了政治生日寄语,并赠送学习书籍,支部每位党员送上了政治生日祝福并畅谈感受。

New JavaScript Set methods are arriving! Since Firefox 127, these methods are available in most major browser engines, which means you won't need a polyfill to make them work everywhere.

This article is a good read for people who are new to Set in JavaScript and are looking to find out how to use these new JavaScript methods. I'll highlight some advantages of using these methods with basic examples to show why you might reach for these instead of building your own implementations.

What's new in Set methods?

For those of you looking for the TL;DR, here's the gist of new methods that have cross-browser support:

  • intersection() returns a new set with elements in both this set and the given set.
  • union() returns a new set with all elements in this set and the given set.
  • difference() returns a new set with elements in this set but not in the given set.
  • symmetricDifference() returns a new set with elements in either set, but not in both.
  • isSubsetOf() returns a boolean indicating if all elements of a set are in a specific set.
  • isSupersetOf() returns a boolean indicating if all elements of a set are in a specific set.
  • isDisjointFrom() returns a boolean indicating if this set has no elements in common with a specific set.

If you've read (or skimmed) through the above list and are confused, don't worry, we'll describe what they do in the following sections. Each of these methods are used to check what the contents of sets are compared to the contents of another specific set.

What is a JavaScript Set?

A set is similar to an Array, except that each value can only be stored once. For example, we can take a list of items, add them all to a set, and then inspect the results of the set. The list on the right is the contents of the <ol> list on the left, but converted to a set. We have all duplicates removed from the list because we're guaranteed that a set is unique:

This might not look like much in a small example such as this, but it's really convenient to have a built-in way to make unique collections of items, especially over larger data and more complex data types and objects. For example, consider that you can add elements to a set like so:

js
const dogs = new Set();
const yoshi = { name: "Yoshi", personality: "Friendly" };
dogs.add(yoshi);

It's also typically faster to check if an element is in a set as opposed to in an Array, so this fits use cases where you need to keep an eye on performance in larger data sets. You can also compose new sets with specific logical properties based on existing sets, and we'll take a look at some examples of that below.

We'll be covering all of the new methods below, but if you want to see everything you can do with a Set, check the Instance methods documentation.

Union of two sets

This is a good example to start us off if you're not familiar with sets. With the union, we can check what elements are in 'either or both' sets. In the example below, we have two lists and we want to make a third one that contains all items from both lists, but without duplicates:

js
// Create a union of both sets
const unionSet = set1.union(set2);

// List the items in the union
unionSet.forEach((item) => {
  const li = document.createElement("li");
  li.textContent = item;
  unionList.appendChild(li);
});

We're using the HTML textContent of each list item so we have sets of strings, but you can see how this can be powerful if we consider that sets can contain data types like arrays or objects.

This is helpful because we don't need any custom implementations to remove duplicates, do equality checks, or make other comparisons. Once we have elements in a set, we know they're all unique, and the union method is an optimized way to make a third set of (unique) items that appear in 'either or both' sets.

Set intersection

With set intersection, we can check what elements occur in both sets to see what the overlap is. In this example, instead of displaying the intersection set as a third list like we did with union above, we can use it to highlight elements that are 'both sets only':

js
// Make the intersection set
const intersectionSet = set1.intersection(set2);

// Loop through lists and highlight if they're in the intersection
allListItems.forEach((item) => {
  if (intersectionSet.has(item.textContent)) {
    item.className = "match";
  }
});

Set symmetric difference

Before we look at difference, let's look at symmetricDifference which might sound complex, but hopefully the example below demystifies that. What the symmetric difference does is lets us check which elements are in either set, but not both.

js
const setNotBoth = set1.symmetricDifference(set2);

allListItems.forEach((item) => {
  if (setNotBoth.has(item.textContent)) {
    item.className = "match";
  }
});

If this is all new and you're struggling to understand, compare the symmetric difference with the intersection example. You should see that the symmetricDifference method does the opposite logical operation of the intersection.

Set difference

With set difference, we can check which elements are in one set but not in another. For this example, we are creating two new sets instead of one. The first one (set1only) is created with set1.difference(set2) and what we get back is a new set of items that are 'set one only'. We do the same with the new set set2only to find items that are in set two but not in the first one. For each list, we can use the sets created with difference to highlight list items that don't appear in the other list.

js
const set1only = set1.difference(set2);
const set2only = set2.difference(set1);

allListItems.forEach((item) => {
  if (set1only.has(item.textContent)) {
    item.className = "setOneMatch";
  } else if (set2only.has(item.textContent)) {
    item.className = "setTwoMatch";
  }
});

Subset, superset, and disjoint from

The last of the new methods and concepts that we can explore are subset, superset, and 'disjoint from' methods. By now, you're familiar with the set operations that return new sets. The subset, superset, and disjoint from methods don't return new sets, but a Boolean value to indicate certain state or logical checks.

The first two we can look at are subset and superset. Instead of highlighting list items, we have some statements or assertions like "Set one is a subset of set two". We then add some text (TRUE / FALSE) to the list item depending on the result of a check like if (set1.isSupersetOf(set2)):

js
if (set1.isSubsetOf(set2)) {
  oneSubTwo.textContent += " [TRUE]";
} else {
  oneSubTwo.textContent += " [FALSE]";
}

So with subset, we can check if all the items from a set appear in another set or not. With superset, we're doing the opposite, to see if a set has all items from another set, plus some additional items.

The last method for us to look at is isDisjointFrom() where we can find out if two sets have no common elements. The first and second sets below are not disjoint from each other as they have one element ("C") in common. The third set is disjoint from the other two as it has no items in common with either set:

Summary

I hope you enjoyed this post about Set methods and why I think sets are an interesting concept to understand. Do you know different ways to use these methods in real-world examples? Feel free to get in touch with us and let me know what you think or if I've missed something! You should be all Set for your next project, so see you next time!

梅毒滴度是什么意思 时间短吃什么药 坐月子吃什么下奶最快最多最有效 什么食物吃了会胀气 最贵的烟是什么
中阴身是什么意思 做梦梦见掉头发是什么意思 维生素b族什么牌子的好 绿茶有什么好处 大学211和985是什么意思
什么盐好 12月份是什么星座 eb病毒抗体阳性是什么意思 那天离开你是什么歌 鼻子出血是什么原因
肌张力高对宝宝有什么影响 用什么梳子梳头发最好 血脂高饮食应注意什么 wonderflower是什么牌子 吃脆骨有什么好处
缩阳是什么意思hcv9jop3ns7r.cn 慢性胰腺炎有什么症状96micro.com 吃雪燕有什么好处hcv8jop5ns4r.cn 独美是什么意思hcv9jop0ns1r.cn 减肥挂什么科youbangsi.com
铜绿是什么hcv9jop5ns4r.cn 康熙雍正乾隆是什么关系hcv9jop8ns0r.cn 陈皮和橘子皮有什么区别hcv8jop8ns4r.cn 什么是疣体hcv8jop2ns4r.cn 8月份是什么季节hcv9jop3ns1r.cn
农业户口和非农业户口有什么区别hcv8jop7ns7r.cn 红豆与赤小豆有什么区别zhiyanzhang.com 梦到镯子碎了什么预兆hcv9jop3ns8r.cn 苦瓜泡水喝有什么功效和作用hcv9jop0ns7r.cn 卡介苗为什么会留疤hcv9jop0ns4r.cn
atp 是什么0297y7.com 面部肌肉跳动是什么原因sscsqa.com xswl什么意思hcv9jop3ns2r.cn 关元穴在什么位置1949doufunao.com 乐话提醒业务是什么意思aiwuzhiyu.com
百度