aluckdog
isContained =(a, b)=>{ if(!(a instanceof Array) || !(b instanceof Array)) return false; if(a.length != b.length) return false; for(var i = 0, len = b.length; i < len; i++){ if(a.indexOf(b[i]) == -1 && b.indexOf(a[i]) == -1) { return false; } } return true; } arr1 = [1,2,3,4,'obj']; arr2 = [4,3,2,1,'obj']; alert(isContained(arr1,arr2));function isContained (a, b){ if(!(a instanceof Array) || !(b instanceof Array)) return false; if(a.length != b.length) return false; for(var i = 0, len = b.length; i < len; i++){ if(a.indexOf(b[i]) == -1 && b.indexOf(a[i]) == -1) { return false; } } return true; } arr1 = [1,2,3,4,'obj']; arr2 = [4,3,2,1,'obj']; alert(isContained(arr1,arr2));
收到一只叮咚
function compare(arr1, arr2) { //临时数组,用于对比删除项 let tempArr = [...arr2] if (arr1 instanceof Array && arr2 instanceof Array) { return arr1.length == arr2.length && arr1.every((item_1) => { //临时数组需要删除的索引 let delIndex = 0 //是否对比成功 let result = false if (isNaN(item_1)) { result = tempArr.some((item_2, index) => { if (isNaN(item_2)) { delIndex = index return true } return false }) } else { delIndex = tempArr.indexOf(item_1) result = delIndex != -1 } tempArr.splice(delIndex, 1) return result }) } else { return false }}var obj = {}var arr = []var nan = NaNvar a = [1, 2, 3, obj, arr, nan]var b = [obj, arr, 3, 2, 1, nan]console.log(compare(a, b))