JavaScript 数组中找出相同字符串元素

发布网友 发布时间:2022-04-23 16:36

我来回答

1个回答

热心网友 时间:2022-04-22 20:03

如果数组中个元素是字符串,这么做方便,如果不是,那就用@lyw_2008_happy的方法就行<br><br>var arr1 = ['a','b','c','d']; <br>var arr2 = ['x','b','c','y'];<br>var long = arr1.length<arr2.length?arr2:arr1;<br>var short = arr1.length<arr2.length?arr1:arr2;<br>var str = ","+long.toString()+",";<br>var result=[];<br>for(var i in short){<br> if(str.indexOf(","+short[i]+",")>=0){<br> result.push(short[i]);<br> }<br>}<br>alert(result.toString());

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com