js判断两个数组的值是否相等 值的顺序可能不同?

请问有什么好的办法可以判断两个数组的值是否相等吗
比如: a = ['a','b','c','d']; b=['b','c','d','a'];
顺序不同但是只要值是相同的话就返回true就可以了 谢谢大家

哈士奇WWW
浏览 1974回答 2
2回答

aluckdog

isContained =(a, b)=>{&nbsp; &nbsp; &nbsp; &nbsp; if(!(a instanceof Array) || !(b instanceof Array)) return false;&nbsp; &nbsp; &nbsp; &nbsp; if(a.length != b.length) return false;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for(var i = 0, len = b.length; i < len; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(a.indexOf(b[i]) == -1 && b.indexOf(a[i]) == -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; arr1 = [1,2,3,4,'obj'];&nbsp; &nbsp; arr2 = [4,3,2,1,'obj'];&nbsp; &nbsp; alert(isContained(arr1,arr2));function isContained (a, b){&nbsp; &nbsp; &nbsp; &nbsp; if(!(a instanceof Array) || !(b instanceof Array)) return false;&nbsp; &nbsp; &nbsp; &nbsp; if(a.length != b.length) return false;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for(var i = 0, len = b.length; i < len; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(a.indexOf(b[i]) == -1 && b.indexOf(a[i]) == -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; arr1 = [1,2,3,4,'obj'];&nbsp; &nbsp; arr2 = [4,3,2,1,'obj'];&nbsp; &nbsp; alert(isContained(arr1,arr2));

收到一只叮咚

function compare(arr1, arr2) {&nbsp; &nbsp; //临时数组,用于对比删除项&nbsp; &nbsp; let tempArr = [...arr2]&nbsp; &nbsp; if (arr1 instanceof Array && arr2 instanceof Array) {&nbsp; &nbsp; &nbsp; &nbsp; return arr1.length == arr2.length && arr1.every((item_1) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //临时数组需要删除的索引&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let delIndex = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //是否对比成功&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let result = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isNaN(item_1)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = tempArr.some((item_2, index) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isNaN(item_2)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delIndex = index&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delIndex = tempArr.indexOf(item_1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = delIndex != -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempArr.splice(delIndex, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; }}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))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JQuery