我这里只比较了简单的如果一维数组:
var arr1 = ['5',2,3,6,'9',8]; var arr2 = [5,6,2,8,9]; var flag = arr1.length >= arr2.length ? true : false; var shortLength = flag ? arr2.length : arr1.length; var shortArr = flag ? arr2 : arr1; var longArr = flag ? arr1 : arr2; var kongArr = []; for(var i=0;i<shortLength;i++){ let n = i let flag = false; var commonIndex = longArr.findIndex(function(item){ return item === shortArr[n] }) if(commonIndex >= 0){ // var longType = Object.prototype.toString.call(longArr[commonIndex]); // var shortType = Object.prototype.toString.call(shortArr[i]) // if(longType == shortType ){ // console.log(1) kongArr.push(longArr[commonIndex]) // } } } console.log(kongArr)
如果我现在要让如果数组里面的引用类型的key和value相等,也算是相同元素呢?要怎么细化,更简洁的写这个方法?例如arr1 = [{'one':1}];arr2 = [{'one':1}]