已知 arr1 = ['a','b','c','d','a','c'],arr2=['x','b','c','y','c','a'],用js取两个数组相同的元素,返回一个新数组,数组要求没有重复元素
//js代码如下:
1 function fnTogether(arr1,arr2){
2 var newArr = [];
3 for (var i = 0; i < arr1.length; i++) {
4 for(var j = 0; j < arr2.length; j++){
5 if(arr1[i] == arr2[j]){
6 if(newArr.indexOf(arr1[i]) == -1){
7 newArr.push(arr1[i]);
8 }
9 }
10 }
11 }
12 return newArr;
13 }
14 alert(fnTogether(arr1,arr2));
简约不简单,代码追求极致