标签:amp filter 去重 row cal 测试的 let 筛选 ras
比较两个数组,然后返回一个新数组,该数组的元素为两个给定数组中所有独有的数组元素。换言之,返回两个数组的差异。
diff([1, 2, 3, 5], [1, 2, 3, 4, 5])
应该返回一个数组。 ["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]
应该返回 ["pink wool"]
。 ["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]
应该返回 ["diorite", "pink wool"]
。["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]
应该返回 []
。 [1, 2, 3, 5], [1, 2, 3, 4, 5]
应该返回 [4]
。[1, "calf", 3, "piglet"], [1, "calf", 3, 4]
应该返回["piglet", 4]
。[], ["snuffleupagus", "cookie monster", "elmo"]
应该返回 ["snuffleupagus", "cookie monster", "elmo"]
。[1, "calf", 3, "piglet"], [7, "filly"]
应该返回 [1, "calf", 3, "piglet", 7, "filly"]
。前面做过查询去重,因此这里考虑运用之前的函数。
function differ(arr1,arr2){
var newArr=arr1.concat(arr2); //将arr1和arr2合成一个新的数组来使用,分边用arr1和arr2在newArr中查询;
var temp=newArr.filter(function(item,index,array){
return arr1.indexOf(item)==-1 ||arr2.indexOf(item)==-1; //newArr在arr1或arr2中找不到的元素就是需要的。
});
return temp;
}
标签:amp filter 去重 row cal 测试的 let 筛选 ras
原文地址:http://www.cnblogs.com/skyyo/p/7613546.html