2 回答
TA贡献1847条经验 获得超7个赞
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));
TA贡献1821条经验 获得超4个赞
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 = NaN
var a = [1, 2, 3, obj, arr, nan]
var b = [obj, arr, 3, 2, 1, nan]
console.log(compare(a, b))
- 2 回答
- 0 关注
- 1973 浏览
添加回答
举报