JavaScript 中 2个数组对比取 相同的元素,代码该怎么写
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 中 2个数组对比取 相同的元素,代码该怎么写相关的知识,希望对你有一定的参考价值。
javascript 中 2个数组对比取 相同的元素,代码该怎么写,用按钮执行对比,谢谢!
如果数组中个元素是字符串,这么做方便,如果不是,那就用@lyw_2008_happy的方法就行
var arr1 = ['a','b','c','d'];var arr2 = ['x','b','c','y'];
var long = arr1.length<arr2.length?arr2:arr1;
var short = arr1.length<arr2.length?arr1:arr2;
var str = ","+long.toString()+",";
var result=[];
for(var i in short)
if(str.indexOf(","+short[i]+",")>=0)
result.push(short[i]);
alert(result.toString()); 参考技术A var arry1 = ['a','b','c','d'];
var arry2 = ['x','b','c','y'];
var arry3 = new Array();
var j = 0;
for(var i=0;i<arry1.length;i++)
for(var k=0;k<arry2.length;k++)
if(arry1[i]==arry2[k])
arry3[j]=arry1[i];
++j;
//array3中存放的就是['b','c']
return array3; 参考技术B
<button onclick="doclick()">按钮</button>
<script type="text/javascript">
function doclick()
//调用一楼的代码
</script>
Python的元组()与字典 { }
1、 tuple操作
我们在定义变量之前,最好先申明该变量的类型,如
l=list() # l为列表
print(l)
t=tuple() # t为元组
print(t)
当我们定义一个相同元素时,不一样的写法将得到不一样的数据类型
a1=(1)
a2=(1,)
print(type(a1))
# <class ‘int‘>
print(type(a2))
# <class ‘tuple‘>
在tuple类型中,单个元素一定要加“,”逗号,否则无法识别为tuple类型。
m = (1,2,3,43,4,6,1,3,4,4)
# count(value) 统计value的个数
print(m.count(1))
# 2
# index(value) 返回第一个value元素的下标
print(m.index(4))
# 4
print(m.index(2))
# 1
2、 dict字典
字典是我们在其他应用中用到的keys:values形式的一种表达形式,字典可以存储任意的对象,也可以是不同的数据类型。
# 字典的三种定义方式
d1 = dict(name = "zhou",age = 22)
print(d1)
# {‘name‘: ‘zhou‘, ‘age‘: 22}
d2 = {"id":43245232,"name":"zhoumoumou"}
print(d2)
# {‘id‘: 43245232, ‘name‘: ‘zhoumoumou‘}
d3 = dict([("ip","1.1.1.1"),("address","ChangSha")])
print(d3)
# {‘ip‘: ‘1.1.1.1‘, ‘address‘: ‘ChangSha‘}
方法:
# get(key) 根据key获取value
print(d1.get("name"))
# zhou
print(d1.get("address"))
# None
# setdefault 根据key获取value,如果key不存在,可以设定默认的value
print(d1.setdefault("name"))
# zhou
print(d1.setdefault("address","ChangSha"))
# ChangSha
# 获取所有的keys值
print(d2.keys())
# dict_keys([‘id‘, ‘name‘])
print(type(d2.keys()))
# <class ‘dict_keys‘>
# 获取所有的values值
print(d2.values())
# dict_values([43245232, ‘zhoumoumou‘])
print(type(d2.values()))
# <class ‘dict_values‘>
for x,y in d3.items():
print("key = {0},value = {1}".format(x,y))
# key = ip,value = 1.1.1.1
# key = address,value = ChangSha
update 和list中的 + 类似
l=list() l+=[1,2,3,4]
m=dict()
n=dict(name="zhou",age=12)
m.update(n)
print(m)
# {‘name‘: ‘zhou‘, ‘age‘: 12}
# l=list() l+=[1,2,3,4]
l=list()
m = [1,2,3,4,5,6]
l+=m
print(l)
# [1, 2, 3, 4, 5, 6]
print(d3)
# pop(key) 删除key所对应的元素
keyDelete = d3.pop("ip")
print(keyDelete)
print(d3)
3、 其他常用操作
help() ctrl + 鼠标左键
s="dedwefwfrgwr"
# help(s.split())
result = s.startswith("de")
print(result)
# True
# dir()
print(dir(s))
# [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘,
# ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘,
# ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘,
# ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘,
# ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘,
# ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘,
# ‘__str__‘, ‘__subclasshook__‘, ‘capitalize‘, ‘casefold‘, ‘center‘,
# ‘count‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘,
# ‘format_map‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdecimal‘, ‘isdigit‘,
# ‘isidentifier‘, ‘islower‘, ‘isnumeric‘, ‘isprintable‘, ‘isspace‘,
# ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘maketrans‘,
# ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘,
# ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘,
# ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘]
# type()
a="123"
print(type(a))
# <class ‘str‘>
print(type(int(a)))
# <class ‘int‘>
# isinstance(a,type) 返回值是一个bool类型
print(isinstance(s,str))
# True
print(isinstance(s,dict))
# False
以上是关于JavaScript 中 2个数组对比取 相同的元素,代码该怎么写的主要内容,如果未能解决你的问题,请参考以下文章