// const uniqueArray = array => [...new Set(array)].sort((a,b)=> a-b)
// console.log(uniqueArray([11,2,2,3]))
// const truncateString = (str , length) => str.length > length? `${str.slice(length)}...`: str;
// console.log(truncateString("qwerty",3))
// const mergeArray = (...arrays) => [...new Set([].concat(...arrays))].sort((a,b)=>a-b);
// console.log(mergeArray([1,2,3],[3,4,5]))
// const longestWord = sentence => sentence.split(' ').reduce((longword, item)=> longword.length > item.length ? longword : item ,'')
// console.log(longestWord('sdfsdf erew aaaaaa, ererw'))
// const isEmptyObject = obj => Object.keys(obj).length == 0;
// console.log(isEmptyObject({'name':111}))
// const average = arr => arr.reduce( (sum , item) => sum +item ,0) / arr.length;
// console.log(average([1,2,3,4,5]))
// const objectToQueryParams = obj => Object.entries(obj).map(([key,value])=>`${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join('&')
// console.log(objectToQueryParams({'name':'fzg', 'sex':'男'}))
// const removeWhitespace = str => str.replace(/\s/g, '')
// console.log(removeWhitespace("H el l o"))
// const isLeapYear = year => (year %4 ==0 && year%100 !==0) || (year%400 ==0)
// console.log(isLeapYear(2004))
// navigator.clipboard.writeText
// const isValidDate = date => date instanceof Date && !isNaN(date)
// console.log(isValidDate(new Date('08-10-2024')))
// const currentTime = () => new Date().toLocaleTimeString(
// ["zh-CN", "en-US"], {hour: '2-digit',minute: '2-digit',second: '2-digit',hour12:false}
// );
// console.log(currentTime())
// const arrEqual = (arr1, arr2) => JSON.stringify(arr1) ===JSON.stringify(arr2);
// console.log(arrEqual([1, 2, 3],[1, 2, 3]));
function deepEqual(val1, val2) {
// 检查引用相等
if (val1 === val2) return true;
// 检查是否为 null
if (val1 == null || val2 == null) return false;
const type1 = typeof val1;
const type2 = typeof val2;
// 检查类型是否相同
if (type1 !== type2) {
return false;
}
// 如果是数组
if(Array.isArray(val1) && Array.isArray(val2)){
// 比较长度
if(val1.length !== val2.length) return false;
// 对数组进行排序,比较无序的情况
const sorted1 = [...val1].sort((a,b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
const sorted2 = [...val2].sort((a,b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
//递归比较每个元素
return sorted1.every((value,index) => deepEqual(value, sorted2[index]));
}
// 如果是对象
if(type1 === 'object' && type2 === 'object'){
const keys1 = Object.keys(val1);
const keys2 = Object.keys(val2);
// 检查键的数量是否相等
if(keys1.length != keys2.length) return false;
// 递归比较每个属性
return keys1.every(key => {
//确保val2中也有该键
if(!keys2.includes(key)) return false;
// 递归比较值
return deepEqual(val1[key] , val2[key])
})
}
//其他
return false;
}
//console.log(deepEqual( [1, 2, {b: 4},{ a: 3 }], [2, 1, { a: 3 },{b:4}] )); // false
//console.log(deepEqual( {name: 'fzg', addr:[1, 2, { a: 3 }]}, {addr:[2, 1, { a: 3 }], name:'fzg'} )); // false
console.log(deepEqual( [{ a: 3,c:[7,8,{d:[9,10,{e:{'f':'1111',g:'2222'}}]}]} , {aa:1999}], [{aa:1999}, { a: 3,c:[7,8,{d:[10,9,{e:{g:'2222','f':'1111'}}]}] }] )); // false
console