chronosmaker
2020-02-04 11:44
function arraysSimilar(arr1, arr2) { if (!(arr1 instanceof Array && arr2 instanceof Array)) { return false; } if (arr1.length !== arr2.length) { return false; } return iterateArr(arr1) === iterateArr(arr2); } function iterateArr(arr) { return arr.map(getType).sort().join(""); } function getType(data) { if (typeof data === "object") { return Object.prototype.toString.call(data); } return typeof data; }
我是这么写的,注意日期类型和null就行了
arr1 = arr1.map(ele => { return ele === null ? 'null' : ele instanceof Date ? 'date' : typeof ele }).sort() arr2 = arr2.map(ele => { return ele === null ? 'null' : ele instanceof Date ? 'date' : typeof ele }).sort() const result = arr1.every((element,index) => element===arr2[index]) return result
JavaScript深入浅出
281100 学习 · 1020 问题
相似问题