正確答案在此
? ? ? ? function arraysSimilar(arr1, arr2){
? ? ? ? ? ? if (arr1.length !== arr2.length) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? for (el1 of arr1) {
? ? ? ? ? ? ? ? ? ? let type1 = Object.prototype.toString.call(el1);
? ? ? ? ? ? ? ? ? ? let stepPaired = false;
? ? ? ? ? ? ? ? ? ? for (el2 of arr2) {
? ? ? ? ? ? ? ? ? ? ? ? let type2 = Object.prototype.toString.call(el2);
? ? ? ? ? ? ? ? ? ? ? ? if (type1 === type2) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? const index = arr2.indexOf(el2);
? ? ? ? ? ? ? ? ? ? ? ? ? ? arr2.splice(index,1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? stepPaired = true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? if (!stepPaired) {
? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? return true;
? ? ? ? }
2021-12-01
? ?/*
?* param1 Array
?* param2 Array
?* return true or false
?*/
function arraysSimilar(arr1, arr2) {
if (
Object.prototype.toString.apply(arr1) === '[object Array]' &&
Object.prototype.toString.apply(arr2) === '[object Array]' &&
arr1.length === arr2.length
) {
var arr_1 = [];
var arr_2 = [];
for (var i = 0; i < arr1.length; i++) {
arr_1[i] = Object.prototype.toString.apply(arr1[i]);
arr_2[i] = Object.prototype.toString.apply(arr2[i]);
}
arr_1 = arr_1.sort();
arr_2 = arr_2.sort();
if (JSON.stringify(arr_1) === JSON.stringify(arr_2)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
//兄臺的代碼,我閱讀之后覺得有問題,如果兩個數(shù)組元素的類型是一致的,但是由于順序不同就會運行失??!我運行了一下試試,確實沒成功!
2021-05-20