function arraysSimilar(arr1, arr2){ var count = 0; if (arr1.length === arr2.length) { arr1.forEach(function (x) { switch(typeof x) { case "number": case "string": case "boolean": case "undefined": case "function": var result = arr2.some(item => (typeof item) === (typeof x)); if (result === true) { count++; } break; case "object": var result = objRec(x); result = arr2.some(item => item === result); if (result === true) { count++; } break; } }); if (count === arr1.length) { return true; } else { return false; } } return false; } function objRec(x) { if (x === null) { return null; } else if (x instanceof Date) { return Date; } else if (x != null && x === x.window) { return window; } }
行吧...我自己找到问题了,貌似我代码哪里出了问题,testData.js的json读取出错。
修改了下,运行成功,显示通过。
function arraysSimilar(arr1, arr2){ var count = 0; if (!(arr1 instanceof Array) || !(arr2 instanceof Array)) { return false; } if (arr1.length === arr2.length) { arr1.forEach(function (x) { switch(typeof x) { case "number": case "string": case "boolean": case "undefined": case "function": var result = arr2.some(item => (typeof item) === (typeof x)); if (result === true) { count++; } break; case "object": if (x instanceof Date) { if(arr2.some(item => item instanceof Date)) { count++; } } else { var result = objRec(x); result = arr2.some(item => item === result); if (result === true) { count++; } } break; } }); if (count === arr1.length) { return true; } else { return false; } } return false; } function objRec(x) { if (x === null) { return null; } else if (x instanceof Date) { return Date(); } else if (x != null && x === x.window) { return window; } else if (x instanceof Array) { return Array; } else { return Object; } }