找出两个数组的共同点 [英] Finding what is common to two arrays

查看:31
本文介绍了找出两个数组的共同点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法比较两个数组并显示它们的共同点?

Is there a way to compare two arrays and show what is common to both of them?

array1 = ["pig", "dog", "cat"]
array2 = ["dog", "cat", "pig", "horse"]

我输入什么来表明 ["pig", "dog", "cat"] 在这两个数组之间是通用的?

What do I type to show that ["pig", "dog", "cat"] are common between these two arrays?

推荐答案

您可以使用 & 对数组进行交集:

You can intersect the arrays using &:

array1 & array2

这将返回["pig", "dog", "cat"].

这篇关于找出两个数组的共同点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆